如何在rails中插入f.select中的选定值

时间:2016-11-25 07:53:56

标签: mysql ruby-on-rails

我想在提交表单时将select标签中的选定值插入数据库。

问题是从select标签中选择所有值,但是当我提交表单时,只有第一个值总是插入到数据库中,而不是选择的值。

我的 _form.html.erb 文件

<div class="form-group">
  <label class="control-label">Requirement Type</label>
  <div class="input-group"> <span class="input-group-addon"> <i class="fa fa-globe"></i> </span>
     <%= f.select :job_types, Settings.job_types_for_job_posting.to_a.map { |w| [w.humanize, w] }, {}, class: "form-control" %>
   </div>
</div>

我的枚举在 setting.yml 文件中定义

job_types_for_job_posting: [Contract, Permanent, On Demand, Internal]

我的 jobs_controller.rb 文件是

    def create
    @job = current_user.job_postings.build(params_value)

    respond_to do |format|
      if @job.save
        add_or_update_skills
        format.json { render json: { result: true } }
        format.html { redirect_to root_path, notice: 'Job is posted successfully' }
      else
        format.json { render json: { result: false } }
        format.html { render :new }
      end
    end
  end

    params.require(:job_posting).permit(:title, :description, :salary_from, :salary_to, :experience, :experienceto,
                                        :expected,:key,:sourcing,:client_id, :job_assignment,:status, :no_of_opening, :location, :designation, :responsibilities, :job_skills, :industry_id, :functional_area_id, :role_category_id, :job_role_id, :job_types)
  end 

当我正在创造新工作时,每次只插入&#34;合同&#34;意思是0.任何人都可以告诉我,我哪里错了......

1 个答案:

答案 0 :(得分:1)

(未经测试)我认为你的专栏应该是:

<%= f.select(:job_types, Settings.job_types_for_job_posting.to_a.collect.with_index { |w, i| [w.humanize, i] }, {}, class: "form-control") %>

数组的索引应该是你的值,对吗?