2012-09-17 128 views
0

我檢查了類似問題的一些答案,似乎都表明問題是attr_accessible,但我似乎無法得到這個工作。不能批量分配嵌套屬性

基本上我有用戶&地址模型,並希望將地址屬性分配爲嵌套屬性。但是,當我保存它會導致無法批量分配屬性錯誤如下所示。

我已經在下面的代碼中嘗試了attr_accessible的許多變體,例如:addresses_attributes,:address_attributes,:primary_address_attributes ...我不確定它爲什麼不工作,也許是因爲它在成員模塊聲明中?

任何人都可以幫忙嗎?

class Member::User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :token_authenticatable, :confirmable, 
    # :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable 

    # each user may have zero or many addresses 
    # their primary email is also set as their login id. 
    has_many :addresses 

    has_one :address, :class_name => "Member::Address", :conditions => "is_primary = true" 
    accepts_nested_attributes_for :addresses, :address 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :email, :password, :password_confirmation, :remember_me,  :address_attributes 

    validates_presence_of :email 

end 

我的形式提交嵌套字段(相關部分)

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:class => "form-horizontal"}) do |f| %> 
    <small>Mandatory fields marked *</small><br><br> 
    <% devise_error_messages! %> 
    <% flash.each do |name, msg| %> 
     <div class="alert alert-<%= name == :notice ? "success" : "error" %>"> 
     <a class="close" data-dismiss="alert">×</a> 
     <%= msg %> 
     </div> 
    <% end %> 

    <%= f.fields_for resource.build_primary_address do |address_form| %> 
    <div class="control-group"> 
     <%= address_form.label :first_name, :class => "control-label" %> 
     <div class="controls"> 
      <%= address_form.text_field :first_name, :class => "input-xlarge" %> 
     </div> 
     </div> 

    <div class="control-group"> 
     <%= address_form.label :last_name, :class => "control-label" %> 
     <div class="controls"> 
      <%= address_form.text_field :last_name, :class => "input-xlarge" %> 
     </div> 
     </div> 
    <% end %> 

錯誤表明這一點:

Can't mass-assign protected attributes: member_address 

{"utf8"=>"✓", 
"authenticity_token"=>"HrZJG2krn15veUKJVh6PgEs4wwufaeRhciHcUIn6AaM=", 
"user"=>{"member_address"=>{"first_name"=>"asdf", 
"last_name"=>"asdfsafd"}, 
"email"=>"[email protected]", 
"password"=>"[FILTERED]", 
"password_confirmation"=>"[FILTERED]"}} 

回答

1

爲了軌理解的是,主地址的形式提交是一個嵌套的屬性,你要在這裏明確告訴它的關係:

<%= f.fields_for :address, resource.build_primary_address do |address_form| %> 

。原因是,因爲您的模型名稱的作用域爲Member::,並且Rails從類名稱中派生表單參數名稱,所以在您的params散列中,最終會使用:member_address而不是:address。因爲:在這種情況下地址是一個字段,其中:用戶接受嵌套的屬性,:地址在params散列中應該表示爲:address_attributes,所以確保後者出現在attr_accessible中。

+0

輝煌!我被困在那一個,也許6個小時,我一直試圖找出它,修復它,非常感謝。 –

+0

很高興有幫助! – cdesrosiers

0

添加

attr_accessible :member_address 

到用戶模式。

說明:您希望初始化ActiveRecord實例的每個哈希鍵都必須聲明爲attr_accessible。看着你的哈希鍵有:在初始化用戶實例的哈希

"user"=>{"member_address"=>{ ... }} 

member_address關鍵,因此它必須包含在attr_accessible聲明。

+0

回來了未知的屬性:member_address –

+0

我試過這個建議,它沒有奏效,錯誤與我在上面評論中寫的錯誤。 –

相關問題