ruby on rails - When I click create in activeadmin, I get a mass assignment error -
so have active admin. have customer model , address model. have address nested in customer. when click create customer, mass assignment error.
error
activemodel::massassignmentsecurity::error in admin::customerscontroller#create can't mass-assign protected attributes: address
customer model
class customer < activerecord::base attr_accessible :name, :email, :phone, :addresses_attributes has_many :addresses accepts_nested_attributes_for :addresses, :allow_destroy => true end
address model
class address < activerecord::base attr_accessible :street, :city, :state, :zip, :customer_id belongs_to :customer has_one :customer_id end
customer controller
activeadmin.register customer # menu item menu :label => "customers", :parent => "administration" filter :name filter :created_at filter :updated_at index column :name end form :partial => "form" show :title => :name panel "customer details" attributes_table_for resource row :name row :email row :phone end text_node(render :partial => "admin/addresses/show", :locals => { :address => resource.address }) end end end
views/admin/customers/_form.html.erb
<%= semantic_form_for [:admin, @customer], :builder => activeadmin::formbuilder |f| f.inputs "customer information" f.input :name f.input :email f.input :phone end render :partial => "admin/addresses/form", :locals => { :form => f } f.buttons end %>
views/admin/addresses/_form.html.erb
<%= form.inputs "address" form.semantic_fields_for :address |address| address.inputs :class => "" address.input :street address.input :city address.input :state address.input :zip, as: :string end end end %>
views/admin/addresses/_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>
in address model don't need reference customer_id in attr_accessible method, neither need declare costumer_id.
you have setted relationship between 2 models, using belongs_to , has_may.
make sure have:
t.references :contact
in address migration.
Comments
Post a Comment