Rails 3.2.3: How to mass assign associated models?

可紊 提交于 2019-12-10 11:58:10

问题


I have a rails 3.2.3, I have a form with two nested models, when I try to submit the form, I get this error:

ActiveModel::MassAssignmentSecurity::Error in ExperimentsController#create

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

Here is my models:

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

I can add attr_accessible for a field, but, what about the nested models ?


回答1:


Try adding:

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

In you experiment model.



来源:https://stackoverflow.com/questions/10147198/rails-3-2-3-how-to-mass-assign-associated-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!