Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accept multiple models with a form

I have a basic form setup of a supplier name and email. I would like to nest my attributes for address into this form, but I keep getting the below error

Unpermitted parameter: address

class Supplier < ActiveRecord::Base
  has_many :addresses, dependent: :destroy, as: :addressable    
  accepts_nested_attributes_for :addresses
end

class Address < ActiveRecord::Base
  belongs_to :addressable, polymorphic: true
  belongs_to  :supplier
end

class SuppliersController < ApplicationController

  def allowed_params
    params.require(:supplier).permit(:name, :email, {:address_attributes => [:first_name, :last_name, :address1, :address2, :city, :zip_code, :country_id]})
  end
end

Supplier Form

%fieldset#admin-supplier-names.span-12
  %label Name
  = form.text_field :name
  %label Email
  = form.text_field :email

= form.fields_for :address do |address_fields|
  %li= address_fields.text_field :first_name, placeholder: :first_name.upcase, value: current_user.first_name
  %li= address_fields.text_field :last_name, placeholder: :last_name, value: current_user.last_name
  %li= address_fields.text_field :address1, placeholder: :address_1
  %li= address_fields.text_field :address2, placeholder: :address_2
  %li= address_fields.text_field :city, placeholder: :city
  %li= address_fields.text_field :zip_code, placeholder: :post_code
  %li=address_fields.select   :country_id, select_countries, :include_blank => true
like image 967
Boss Nass Avatar asked May 18 '16 09:05

Boss Nass


2 Answers

Unpermitted parameter: address

With your associations, you need to change address to addresses in fields_for and allowed_params.

= form.fields_for :addresses do |address_fields|

And in the controller's allowed_params

def allowed_params
  params.require(:supplier).permit(:name, :email, :addresses_attributes => [:first_name, :last_name, :address1, :address2, :city, :zip_code, :country_id])
end
like image 197
Pavan Avatar answered Sep 25 '22 00:09

Pavan


Your association declaration between supplier and address and the form code are out of sync.

If your supplier can have only one address, then update your code where applicable with the following:

class Supplier < ActiveRecord::Base
  has_one :address, as: :addressable, dependent: :destroy
  accepts_nested_attributes_for :address
end

class Address < ActiveRecord::Base
  belongs_to :addressable, polymorphic: true
end

class SuppliersController < ApplicationController

  def new
    @supplier = Supplier.new
    @supplier.build_address
  end

  def edit
     @supplier = Supplier.find(params[:id])
     @supplier.build_address if @supplier.address.nil?
  end

  def allowed_params
    params.require(:supplier).permit(:name, :email, address_attributes: [:first_name, :last_name, :address1, :address2, :city, :zip_code, :country_id])
  end
end

%fieldset#admin-supplier-names.span-12
  %label Name
  = form.text_field :name
  %label Email
  = form.text_field :email

= form.fields_for :address do |address_fields|
  %li= address_fields.text_field :first_name, placeholder: :first_name.upcase, value: current_user.first_name
  %li= address_fields.text_field :last_name, placeholder: :last_name, value: current_user.last_name
  %li= address_fields.text_field :address1, placeholder: :address_1
  %li= address_fields.text_field :address2, placeholder: :address_2
  %li= address_fields.text_field :city, placeholder: :city
  %li= address_fields.text_field :zip_code, placeholder: :post_code
  %li=address_fields.select   :country_id, select_countries, :include_blank => true

But if your supplier can have many addresses, then update your code where applicable with the following:

class Supplier < ActiveRecord::Base
  have_many :addresses, as: :addressable, dependent: :destroy
  accepts_nested_attributes_for :addresses
end

class Address < ActiveRecord::Base
  belongs_to :addressable, polymorphic: true
end

class SuppliersController < ApplicationController

  def new
    @supplier = Supplier.new
    3.times { @supplier.addresses.build } #setup as many as you want
  end

  def edit
     @supplier = Supplier.find(params[:id])
     addresses_count = (3 - @supplier.addresses.count) 
     addresses_count.times { @supplier.addresses.build }
  end

  def allowed_params
    params.require(:supplier).permit(:name, :email, addresses_attributes: [:first_name, :last_name, :address1, :address2, :city, :zip_code, :country_id])
  end
end

%fieldset#admin-supplier-names.span-12
  %label Name
  = form.text_field :name
  %label Email
  = form.text_field :email

= form.fields_for :addresses do |address_fields|
  %li= address_fields.text_field :first_name, placeholder: :first_name.upcase, value: current_user.first_name
  %li= address_fields.text_field :last_name, placeholder: :last_name, value: current_user.last_name
  %li= address_fields.text_field :address1, placeholder: :address_1
  %li= address_fields.text_field :address2, placeholder: :address_2
  %li= address_fields.text_field :city, placeholder: :city
  %li= address_fields.text_field :zip_code, placeholder: :post_code
  %li=address_fields.select   :country_id, select_countries, :include_blank => true

Refer to StrongParameters, FormBuilder for more info.

like image 39
Dharam Gollapudi Avatar answered Sep 24 '22 00:09

Dharam Gollapudi