2013-05-01 32 views
1

前言:我正在嘗試爲具有嵌套地址表單的客戶創建。一旦點擊創建客戶,我得到這個錯誤。另一個不能大規模分配受保護的屬性:地址職位

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, :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 

地說我已經嘗試了一切是一個謊言,因爲它不會工作,雖然我試圖讓這個工作有一陣子。

+1

「客戶」中沒有任何內容表示它接受其「地址」的嵌套屬性... – 2013-05-01 19:40:08

+0

感謝dave的響應,請參閱下面的註釋。 – DhatchXIX 2013-05-01 19:46:54

+0

任何人都可以幫助我嗎? – DhatchXIX 2013-05-02 12:26:11

回答

2

您必須add

accepts_nested_attributes_for :addresses 
在您的客戶模型

順便說一句,爲什麼錯誤是單數(地址和不地址)?

您還必須將:addresses_attributes添加到attr_accessible調用中。

+0

這實際上是我的第一次猜測,但我得到了同樣的錯誤。在我發佈之前,我一定把它拿出來了。 – DhatchXIX 2013-05-01 19:46:16

+0

請查看我更新的帖子。 – 2013-05-01 19:51:29

+0

我更新了,什麼也沒有 – DhatchXIX 2013-05-01 20:11:41

相關問題