Ruby on rails 还有一个可以';t mass分配受保护的属性:地址栏
前言:我正在尝试创建一个具有嵌套地址表单的客户。单击“创建客户”后,我收到此错误Ruby on rails 还有一个可以';t mass分配受保护的属性:地址栏,ruby-on-rails,activeadmin,Ruby On Rails,Activeadmin,前言:我正在尝试创建一个具有嵌套地址表单的客户。单击“创建客户”后,我收到此错误 ActiveModel::MassAssignmentSecurity::Error in Admin::CustomersController#create Can't mass-assign protected attributes: address 客户模型 class Customer < ActiveRecord::Base attr_accessible :name, :email, :ph
ActiveModel::MassAssignmentSecurity::Error in Admin::CustomersController#create
Can't mass-assign protected attributes: address
客户模型
class Customer < ActiveRecord::Base
attr_accessible :name, :email, :phone, :addresses_attributes
has_many :addresses
accepts_nested_attributes_for :addresses, :allow_destroy => true
end
说我什么都试过了是谎话,因为它不起作用,尽管我已经试了一段时间让它起作用。你必须这样做
在您的客户模型中
顺便问一下,为什么错误是单数的(地址和非地址)
您必须将
:addresses\u attributes
添加到attr\u accessible call中。在Customer
中没有任何内容表明它接受其地址的嵌套属性。
…感谢您的回复dave,请参阅下面我的评论。这实际上是我的第一个猜测,但我得到了相同的错误。我一定是在寄到这里之前把它拿出来的。
class Address < ActiveRecord::Base
attr_accessible :street, :city, :state, :zip, :customer_id
belongs_to :customer
has_one :customer_id
end
ActiveAdmin.register Customer, :sort_order => "name_asc" do
# Menu item
menu :label => "Customers", :parent => "Administration"
filter :name
filter :created_at
filter :updated_at
action_item :only => [:show] do
link_to "Contacts", client_contacts_path( resource )
end
index do |t|
selectable_column
column(:name, sortable: :name) { |customer| link_to truncate(customer.name, length: 35), customer, title: customer.name }
column "Created", :sortable => :created_at do |customer|
customer.created_at.humanize
end
column "Updated", :sortable => :updated_at do |customer|
customer.updated_at.humanize
end
column "" do |customer|
restricted_default_actions_for_resource(customer) + link_to( "Contacts", client_contacts_path(customer), :class => "member_link" )
end
end
form :partial => "form"
show :title => :name do
panel "Customer Details" do
attributes_table_for resource do
row :name
row :email
row :phone
row :created_at do
resource.created_at.humanize
end
row :updated_at do
resource.updated_at.humanize
end
end
text_node(render :partial => "admin/addresses/show", :locals => { :address => resource.address })
end
end
end
accepts_nested_attributes_for :addresses