如何存储课程以外的下拉菜单和打开新的文本框放其他教育

How to store course other than dropdown menu and open new textbox put other educations?

本文关键字:文本 其他 下拉菜单 存储 何存储      更新时间:2023-09-26

模型领域-学位,研究所,百分比这些是模型领域。

如果课程不是下拉菜单,那么选择"其他"并显示输入用户教育的文本框。当表单提交时,我想在学位列中存储教育。但是现在我不能处理这两个值。

education.html。erb文件

  <p>
    <%= f.label :user_id, "Select User.*" %>
    <% if params[:user_id] != nil %>
      <%= f.select :user_id, :collection => User.where(:id => params[:user_id]).map{|u| ["#{u.firstname}, #{u.lastname}", u.id]} %>
    <% else  %>
      <%  @selected_user_id = EducationInformations.where(:id => params[:id]).pluck(:user_id).first  %>
      <%= f.select :user_id, :collection => User.where(:id => @selected_user_id).map{|u| ["#{u.firstname}, #{u.lastname}", u.id]} %>
    <% end  %>  
  </p>   
  <br/> 
  <% course = ["MCA","BCA","BSC","Engg","Diploma in computer science","Other"] %>
  <p>
    <%= f.label :degree, "Select Course.*" %>
    <%= f.select :degree , :collection => (course) %>
  </p>
  <div id="other_edu">
    <br/>
    <p>
      <%= f.label :other_degree, "Enter your course" %>
      <%= f.text_field :other_degree , :id => "other_course", :onblur => "getCourse()" %>
    </p>
  </div>
  <br/>
  <p>
    <%= f.label :institute, "Institute*" %>
    <%= f.text_field :institute %>
  </p>
  <br/>
  <p>
    <%= f.label :percentage, "Grade/Percentage" %>
    <%= f.text_field :percentage %>
  </p> 

javascript
$(document).ready(function() {
    var select = document.getElementById('education_informations_degree');
    var other_course = document.getElementById('other_course');
    $('#other_edu').hide(); 
        select.onchange = function() {
            var selected_course =  select.value;
            if (selected_course == "Other")
            {
                $('#other_edu').show(); 
            }else{
                $('#other_edu').hide(); 
            }
        }
    });
function getCourse() {
        var x = document.getElementById("other_course"); 
        var select = document.getElementById('education_informations_degree');
        select.value = x.value;
        console.log(x.value);
    }

 Model
                                                                                            class EducationInformations < ActiveRecord::Base
      unloadable
       attr_accessible :user_id, :degree, :institute, :passing_year, :country, :university, :percentage
        validates :degree, presence: true
        validates :passing_year, presence: true
        validates :university, presence: true
        validates :institute, presence: true
    end

Controller 
    def create
        @edu_info = EducationInformations.new(educational_params)
            if @edu_info.save
                #flash[:notice] = 'Vote saved.'
                redirect_to @edu_info
            else
                render "new"
            end     
    end
private
    def educational_params
        params.require(:education_informations).permit(:user_id, :degree, :institute, :passing_year, :country, :university, :percentage)
    end

当用户从下拉列表中选择值时,我如何修复此问题,因此我将值设置为文本框值。如果我选择"其他"项目,那么显示文本框和用户可以输入的任何内容,所以我发送文本值。我知道这不是最好的办法,但我认为这个办法能解决我的问题。


视图代码
 <% course = ["MCA","BCA","BSC","Engg","Diploma in computer science","Other"] %>
  <p>
    <%= label_tag "select_degree", "Select Course *" %>
    <%= select_tag "select_degree" , options_for_select(course) %>
  </p>
  <div id="other_edu">
    <br/>
    <p>
      <%= f.label :degree, "Enter your course *" %>
      <%= f.text_field :degree  %>
    </p>
  </div>
  <br/>
  <p>
    <%= f.label :institute, "Institute *" %>
    <%= f.text_field :institute %>
  </p>
  <br/>
  <p>
    <%= f.label :percentage, "Grade/Percentage " %>
    <%= f.text_field :percentage %>
  </p>

Javascript

$(document).ready(function() {
    var select = document.getElementById('select_degree');
    var other_course = document.getElementById('education_informations_degree');
    $('#other_edu').hide(); 
    select.onchange = function() {
        var selected_course =  select.value;
        if (selected_course == "Other")
        {
            $('#other_edu').show(); 
        }else{
            $('#other_edu').hide(); 
            other_course.value = select.value;
        }
    }
});