Michal
Michal

Reputation: 139

Rails 4 nested form with has_many, through and multiple select

I have a problem with nested form and has_many relation. Bussiness case: there are laboratories and their suppliers. Suppliers can be shared between labs.

Models

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

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

Controller

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

Result of the inspect on params after submitting form:

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"...",
 "lab"=>{"code"=>"L01",
 "name"=>"xxx"},
 "lab_suppliers"=>{"supplier_id"=>["",
 "1",
 "3"]},
 "commit"=>"Create Lab"}

While submitting form I receive ActiveModel::ForbiddenAttributesError on the line:

@lab_supplier = @lab.lab_suppliers.new(params[:lab_suppliers])

What am i missing to make it work as expected?

Upvotes: 3

Views: 3336

Answers (2)

user5900658
user5900658

Reputation:

It seems like you need to explicitly tell lab_params which attributes from lab_suppliers you need to pass like:

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

Try it and let me know.

Upvotes: 2

Michal
Michal

Reputation: 139

Link to other post that helped me to find the working solution: [Rails nested form with multiple entries

Below I provide the working solution showing how to pass values from the multiple select as nested attributes and insert them to the db.

Models

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

Comment: accepts_nested_attributes_for is put only on has_many/has_one side. No need to put it on belongs_to side

Form (Lab)

<%= 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.submit %><% end %>

Controller

Comment: There is no need to permit any additional params in supplier or lab_suppliers controllers

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

Comment: supplier_id: [] in the lab_suppliers_attributes permitts array of values from the multiple dropdown to be passed

Upvotes: 0

Related Questions