Rails 4与has_many,通过和多选择嵌套窗体
问题描述:
我有嵌套窗体和has_many关系的问题。商业案例:有实验室及其供应商。供应商可以在实验室之间共享。Rails 4与has_many,通过和多选择嵌套窗体
模型
class Lab < ActiveRecord::Base
has_many :lab_suppliers
has_many :suppliers, through: :lab_suppliers
accepts_nested_attributes_for :lab_suppliers
end
class Supplier < ActiveRecord::Base
has_many :lab_suppliers
has_many :labs, through: :lab_suppliers
accepts_nested_attributes_for :lab_suppliers
end
class LabSupplier < ActiveRecord::Base
belongs_to :lab
belongs_to :supplier
accepts_nested_attributes_for :lab
accepts_nested_attributes_for :supplier
end
表
<%= form_for(@lab) do |f| %>
<div class="field">
<%= f.label :code %><br>
<%= f.text_field :code %>
</div>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class"field">
<%= fields_for :lab_suppliers do |ff| %>
<%= ff.label :supplier_id %><br>
<%= ff.collection_select :supplier_id, Supplier.all, :id, :name, {include_blank: true}, {:multiple => true, :class=>""} %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
控制器
class LabsController < ApplicationController
before_action :set_lab, only: [:show, :edit, :update, :destroy]
# GET /labs/new
def new
@lab = Lab.new
@lab.lab_suppliers.build
end
# POST /labs
# POST /labs.json
def create
#raise params.inspect
@lab = Lab.new(lab_params)
@lab_supplier = @lab.lab_suppliers.new(params[:lab_suppliers])
@lab_supplier.save
@lab.save
private
def lab_params
params.require(:lab).permit(:code, :name, lab_suppliers_attributes: [])
end
end
结果的检查上PARAMS AFTE [R提交形式:
参数:
{"utf8"=>"✓",
"authenticity_token"=>"...",
"lab"=>{"code"=>"L01",
"name"=>"xxx"},
"lab_suppliers"=>{"supplier_id"=>["",
"1",
"3"]},
"commit"=>"Create Lab"}
在提交表格我加载ActiveModel收到:: ForbiddenAttributesError 就行了:
@lab_supplier = @lab.lab_suppliers.new(params[:lab_suppliers])
我缺少什么,使其工作如预期?
答
好像你需要明确地告诉lab_params
从你需要通过像lab_suppliers
哪些属性:
params.require(:lab).permit(:code, :name, lab_suppliers_attributes: [:supplier_id])
试试吧,让我知道。
答
链接到其他岗位,帮助我找到工作的解决方案: [Rails nested form with multiple entries
下面我提供了展示如何通过从多个值选择嵌套的属性,并将其插入到数据库的工作方案。
模式
class Lab < ActiveRecord::Base
has_many :lab_suppliers#, :foreign_key => 'lab_id', dependent: :destroy
has_many :suppliers, through: :lab_suppliers
accepts_nested_attributes_for :lab_suppliers, :allow_destroy => true
end
class Supplier < ActiveRecord::Base
has_many :lab_suppliers
has_many :labs, through: :lab_suppliers
end
class LabSupplier < ActiveRecord::Base
belongs_to :lab
belongs_to :supplier
end
评论: accepts_nested_attributes_for只装上的has_many/HAS_ONE侧。没有必要把它放在一边belongs_to的
表(实验室)
<%= form_for(@lab) do |f| %>
<div class="field">
<%= f.label :code %><br>
<%= f.text_field :code %>
</div>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class"field">
<%= f.fields_for :lab_suppliers do |ff| %>
<%= ff.label :supplier_id %><br>
<%= ff.collection_select :supplier_id, Supplier.all, :id, :name, {include_blank: true}, {:multiple => true, :class=>""} %>
<% end %>
<%= F。提交%> <%端%>
控制器
评论: 没有必要以允许供应商或lab_suppliers控制器的任何额外PARAMS
class LabsController < ApplicationController
before_action :set_lab, only: [:show, :edit, :update, :destroy]
def new
@lab = Lab.new
@lab.lab_suppliers.build
end
def create
@lab = Lab.new(lab_params)
@startcount=1 #start counting from 1 because the first element in the array of nested params is always null
@lab.lab_suppliers.each do |m|
#raise lab_params[:lab_suppliers_attributes]["0"][:supplier_id][@startcount].inspect
m.supplier_id = lab_params[:lab_suppliers_attributes]["0"][:supplier_id][@startcount]
@startcount +=1
end
respond_to do |format|
if @lab.save
lab_params[:lab_suppliers_attributes]["0"][:supplier_id].drop(@startcount).each do |m|
@lab.lab_suppliers.build(:supplier_id => lab_params[:lab_suppliers_attributes]["0"][:supplier_id][@startcount]).save
@startcount += 1
end
format.html { redirect_to labs_path, notice: 'Lab was successfully created.' }
format.json { render :show, status: :created, location: @lab }
else
format.html { render :new }
format.json { render json: @lab.errors, status: :unprocessable_entity }
end
end
end
private
def lab_params
params.require(:lab).permit(:name, :code, lab_suppliers_attributes: [supplier_id: [] ])
end
end
评论:supplier_id:[]中lab_suppliers_attributes允许从多个下拉列表中传递值的数组
不,我试过了,仍然收到相同的错误。我认为问题在于lab_suppliers params没有嵌套在这里的lab_params – Michal
中:尝试做所以,把f – loloso
你是对的。更改为f.fields_for使lab_suppliers表单值嵌套在lab_params中 – Michal