为什么这个模型没有显示可访问的属性?

时间:2013-02-14 20:41:06

标签: ruby-on-rails rails-activerecord

我有两个模型,PersonBrainPerson has_one :brainBrain belongs_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 

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

我在attr_accessible :weight_kg中有Brain,在Person accepts_nested_attributes_for :brain我有{{1}},所以我(错误地)希望这可行。

我错过了什么?

1 个答案:

答案 0 :(得分:2)

将attr_accessible更改为:

attr_accessible :brain_attributes