2

So i have active admin. I have a customer model and a address model. I have address nested with in customer. When i click create customer, I get a 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 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

views/admin/customers/_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
%>

views/admin/addresses/_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 
%>

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>
DhatchXIX
  • 437
  • 1
  • 3
  • 17
  • With any error, it's really helpful to include the exact error message you get in the question :) – zkcro May 02 '13 at 23:04

2 Answers2

0

In your Address model you don't need to reference the customer_id in the attr_accessible method, neither you need to declare costumer_id.

You have already setted the relationship between the two models, using belongs_to and has_may.

Make sure you have:

t.references :contact

in your address migration.

Paulo Fidalgo
  • 21,709
  • 7
  • 99
  • 115
  • I do not have that in my migration. How do I go about doing that? – DhatchXIX May 02 '13 at 23:08
  • You can edit the migrations file, for a dirt solution, or create a new migration to add a new column. If you are using edge rails (4.0) you can use: rails generate migration AddAddressRefToContacts contact:references as you can see by the docs: http://edgeguides.rubyonrails.org/migrations.html – Paulo Fidalgo May 03 '13 at 08:38
  • I am assuming you mean customer when you say contact – DhatchXIX May 03 '13 at 14:13
0

you do build form for has_one relation, but you have has_many one.

 f.inputs "Addresses" do
        f.has_many :addresses do |t|
             t.input :street 
             t.input :city 
             t.input :state
             t.input :zip, as: :string

      end
end

In this case your models should look like

If you have has_one relation, than change model declaration to

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
end

Look next question /answer for more examples How to use ActiveAdmin on models using has_many through association?

If you have has_one relation, than change model declaration to

Customer model

class Customer < ActiveRecord::Base
   attr_accessible :name, :email, :phone, :address_attributes
  has_one :address
  accepts_nested_attributes_for :address, :allow_destroy => true
end

Address model

class Address < ActiveRecord::Base
   attr_accessible :street, :city, :state, :zip, :customer_id
  belongs_to :customer
end
Community
  • 1
  • 1
Fivell
  • 11,829
  • 3
  • 61
  • 99