DhatchXIX
DhatchXIX

Reputation: 437

yet another Can't mass-assign protected attributes: address post

Preface: I am trying to create a customer for that has a nested addresses form. upon clicking create customer i get this 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

Customers controller

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

To say i have tried everything is a lie because it won't work, though i have tried to get this to work for a while.

Upvotes: 1

Views: 264

Answers (1)

Lazarus Lazaridis
Lazarus Lazaridis

Reputation: 6029

You must add

 accepts_nested_attributes_for :addresses

in your Customer model.

By the way, why the error is in singular (Address and not Addresses)?

You must add :addresses_attributes to the attr_accessible call too.

Upvotes: 2

Related Questions