2013-02-14 55 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