gpt4 book ai didi

mysql - 如何从 f.select in rails 中插入选定值

转载 作者:行者123 更新时间:2023-11-28 23:22:46 24 4
gpt4 key购买 nike

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

问题是所有值都是从 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

当我准备新工作时,每次只插入“契约(Contract)”表示 0。任何人都可以告诉我,我哪里错了......

最佳答案

(没有测试)我认为你的行应该是:

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

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

关于mysql - 如何从 f.select in rails 中插入选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40800099/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com