2013-02-14 74 views
0

我有兩個型號,一個Person和一個BrainPersonhas_one:brainBrainbelongs_to:person。我想通過/ person/update分配Brain屬性。此模型爲何不顯示可訪問的屬性?

class Person < ActiveRecord::Base 
    has_one :brain 
    attr_accessible :name 
    attr_accessible :brain 
    accepts_nested_attributes_for :brain 

end 

class Brain < ActiveRecord::Base 
    belongs_to :person 
    attr_accessible :weight_kg 

    attr_accessible :person 
    accepts_nested_attributes_for :person 
end 

在Rails的控制檯,我可以分配到Person.brain:(並通過控制檯)

> p = Person.first 
=> #<Person id: 1, name: "Dave", created_at: "2013-02-14 20:17:35", updated_at: "2013-02-14 20:17:35"> 

> p.brain.weight_kg = 5.0 
    Brain Load (0.2ms) SELECT "brains".* FROM "brains" WHERE "brains"."person_id" = 1 LIMIT 1 
=> 5.0 
> p.save 
    (0.6ms) begin transaction 
    (0.6ms) UPDATE "brains" SET "weight_kg" = 5.0, "updated_at" = '2013-02-14 20:18:11.010544' WHERE "brains"."id" = 1 
    (317.6ms) commit transaction 
=> true 

通過網頁形式我不能,因爲陳腐錯誤的,「無法mass-assign protected attributes:brain_attributes「。

attr_accessible :weight_kgBrain,並在Person我有accepts_nested_attributes_for :brain,所以我(錯誤地)希望這個工作。

我錯過了什麼?

+0

感謝新星。原來我在這個設置上還有其他問題,但是你的建議讓我知道了。 – 2013-02-15 15:44:43

回答

2

更改attr_accessible到:

attr_accessible :brain_attributes