【发布时间】:2018-10-11 22:21:50
【问题描述】:
我有一个带有嵌套属性的表单。我的嵌套属性都可以正常工作。
但是,我希望选择那些可供用户检查的属性(真/假)以包含在表单的创建中。下面的代码对我不起作用,不确定我缺少什么。
表格
<%= form_with(model: staff, local: true) do |f| %>
<%= f.collection_select :user_id, User.order('last_name'), :id, :full_name %>
<%= f.collection_select :department_id, Department.all, :id, :name %>
<%= f.check_box :sale_checkbox, {}, true, false %><%= f.label "Sales" %><br />
<%= f.check_box :management_checkbox, {}, true, false %><%= f.label "Management" %><br />
<%= f.check_box :summary_checkbox, {}, true, false %><%= f.label "Summary" %>
<% end %>
因此,使用上面的代码,有时 sales 不适用,因此用户会取消选择它,因此它不会包含在表单创建中。
控制器
def create
@staff = Staff.new(staff_params)
if params[:staff][:sale_checkbox] == true
@staff.sales.build
end
if params[:staff][:management_checkbox] == true
@staff.managements.build
end
if params[:staff][:summary_checkbox] == true
@staff.summaries.build
end
end
def staff_params
params.require(:staff).permit(:sale_checkbox, :management_checkbox, :summary_checkbox)
end
型号
class Staff < ApplicationRecord
attr_accessor :sale_checkbox
attr_accessor :management_checkbox
attr_accessor :summary_checkbox
end
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-5