だから私はアクティブな管理者を持っています。顧客モデルと住所モデルがあります。私は顧客にネストされた住所を持っています。[顧客の作成] をクリックすると、一括割り当てエラーが発生します。
エラー
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>