Rails 4嵌套表单,带有has_many,through和multiple select

时间:2016-07-24 22:09:51

标签: ruby-on-rails nested-forms has-many-through has-many multiple-select

我遇到嵌套表单和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的结果:

参数:

{"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])

我错过了什么让它按预期工作?

2 个答案:

答案 0 :(得分:2)

您似乎需要明确告诉lab_params您需要传递来自lab_suppliers的哪些属性:

params.require(:lab).permit(:code, :name, lab_suppliers_attributes: [:supplier_id])

试试并告诉我。

答案 1 :(得分:0)

链接到帮助我找到工作解决方案的其他帖子: [Rails nested form with multiple entries

下面我提供了一个工作解决方案,展示了如何将多个select中的值作为嵌套属性传递并将它们插入到db中。

<强>模型

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 %>

  &lt;%= f.submit%&gt;&lt;%end%&gt;

<强>控制器

注释: 无需在供应商或lab_suppliers控制器中允许任何其他参数

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

注释:lab_suppliers_attributes中的supplier_id:[]允许传递多个下拉列表中的值数组