0

Rails 3.2.3 があり、2 つのネストされたモデルを含むフォームがあります。フォームを送信しようとすると、次のエラーが発生します。

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 に答える