所以我有活跃的管理员。我有一个客户模型和一个地址模型。我有嵌套在客户中的地址。当我单击创建客户时,出现批量分配错误。
错误
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
地址模型
class Address < ActiveRecord::Base
attr_accessible :street, :city, :state, :zip, :customer_id
belongs_to :customer
has_one :customer_id
end
客户控制器
ActiveAdmin.register Customer do
# Menu item
menu :label => "Customers", :parent => "Administration"
filter :name
filter :created_at
filter :updated_at
index do
column :name
end
form :partial => "form"
show :title => :name do
panel "Customer Details" do
attributes_table_for resource do
row :name
row :email
row :phone
end
text_node(render :partial => "admin/addresses/show", :locals => { :address => resource.address })
end
end
end
意见/管理员/客户/_form.html.erb
<%=
semantic_form_for [:admin, @customer], :builder => ActiveAdmin::FormBuilder do |f|
f.inputs "Customer Information" do
f.input :name
f.input :email
f.input :phone
end
render :partial => "admin/addresses/form", :locals => { :form => f }
f.buttons
end
%>
意见/管理/地址/_form.html.erb
<%=
form.inputs "Address" do
form.semantic_fields_for :address do |address|
address.inputs :class => "" do
address.input :street
address.input :city
address.input :state
address.input :zip, as: :string
end
end
end
%>
意见/管理/地址/_show.html.erb
<div class="panel">
<h3>Address</h3>
<div class="panel_contents">
<div id="attributes_table_employee_1" class="attributes_table">
<table cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<th>Street</th>
<td><%= address.street.blank? ? raw('<span class="empty"></span>') : address.street %></td>
</tr>
<tr>
<th>City</th>
<td><%= address.city.blank? ? raw('<span class="empty">Empty</span>') : address.city %></td>
</tr>
<tr>
<th>State</th>
<td><%= address.state.blank? ? raw('<span class="empty">Empty</span>') : address.state %></td>
</tr>
<tr>
<th>Zip</th>
<td><%= address.zip.blank? ? raw('<span class="empty">Empty</span>') : address.zip %></td>
</tr>
</tbody></table>
</div>
</div>
</div>