Rails 3.2.3:如何批量分配关联模型? [英] Rails 3.2.3: How to mass assign associated models?

查看:31
本文介绍了Rails 3.2.3:如何批量分配关联模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 rails 3.2.3,我有一个带有两个嵌套模型的表单,当我尝试提交表单时,出现此错误:

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

这是我的模型:

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,但是,嵌套模型呢?

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

推荐答案

尝试添加:

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

在你的实验模型中.

这篇关于Rails 3.2.3:如何批量分配关联模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆