0

我有两个模型,一个Person和一个BrainPerson has_one :brain, 和Brain belongs_to :person. 我想Brain通过 /person/ 更新分配属性。

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 

通过网络表单(以及通过控制台)我不能,因为陈旧的错误,“无法批量分配受保护的属性:brain_attributes ”。

我有attr_accessible :weight_kgBrain而且Person我有accepts_nested_attributes_for :brain,所以我(错误地)期望这能起作用。

我错过了什么?

4

1 回答 1

2

将 attr_accessible 更改为:

attr_accessible :brain_attributes
于 2013-02-14T20:45:37.507 回答