我有一个供应商名称和电子邮件的基本表单设置。我想将地址的属性嵌套到此表单中,但我一直收到以下错误
未经许可的参数:地址
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
供应商表格
%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
答案 0 :(得分:4)
未经许可的参数:地址
通过您的关联,您需要在address
和addresses
中将fields_for
更改为allowed_params
。
= form.fields_for :addresses do |address_fields|
在控制器的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
答案 1 :(得分:1)
supplier
和address
之间的关联声明与表单代码不同步。
如果您的supplier
只能有一个address
,请使用以下内容更新您的代码:
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
但是,如果您的supplier
可以包含多个addresses
,请在适用的情况下使用以下代码更新您的代码:
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
有关详细信息,请参阅StrongParameters,FormBuilder。
答案 2 :(得分:0)
请查看并更新您的代码,如下所示 - :
clojure.core/*out*
其余的代码很好,只需进行上述更改就可以了......:)