0

我有一个 rails 3.2.3,我有一个包含两个嵌套模型的表单,当我尝试提交表单时,我收到此错误:

ActiveModel::MassAssignmentSecurity::Error in ExperimentsController#create

Can't mass-assign protected attributes: descriptions_attributes, circuits_attributes

这是我的模型:

class Experiment < ActiveRecord::Base
  attr_accessible :title, :intro_text

  has_many :circuits, :dependent => :destroy
  has_many :descriptions, :dependent => :destroy


  accepts_nested_attributes_for :descriptions, :reject_if => lambda { |a| a[:data].blank? }, :allow_destroy => true
  accepts_nested_attributes_for :circuits, :reject_if => lambda { |a| a[:data].blank? }, :allow_destroy => true

end

class Circuit < ActiveRecord::Base
  attr_accessible :data, :title

  belongs_to :experiment
end

class Description < ActiveRecord::Base
  attr_accessible :data, :title

  belongs_to :experiment
end

我可以添加attr_accessible一个字段,但是,嵌套模型呢?

4

1 回答 1

3

尝试添加:

class Experiment < ActiveRecord::Base
  attr_accessible :title, :intro_text, :descriptions_attributes, :circuits_attributes
  [...]

在你的实验模型中。

于 2012-04-13T19:41:28.150 回答