Rail3 attr_accessbile 用于嵌套模型

发布于 2024-11-10 02:02:28 字数 797 浏览 0 评论 0原文

我很难理解为什么这些属性仍然受到保护。我应该使用父模型中的范围来允许这些属性吗?

# Email model

class EmailAddress < ActiveRecord::Base
  belongs_to :emailable, :polymorhpic => true

  validates_presence_of :address
  validates_format_of :address, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i

  attr_accessible :address, :location
end

# Company model

class Company < ActiveRecord::Base
  has_many :email_addresses, :as => :emailable, :dependent => :destroy

  validates_presence_of :company_name

  accepts_nested_attributes_for :email_addresses, :reject_if => proc { |attributes| attributes[:address].blank? }

  attr_accessible :company_name, :background, :email_addresses_attributes
end

# Error log

WARNING: Can't mass-assign protected attributes: address, location

I am struggling to see why the attributes are still protected. Is there a scope within the parent model that I should be using to allow those attributes?

# Email model

class EmailAddress < ActiveRecord::Base
  belongs_to :emailable, :polymorhpic => true

  validates_presence_of :address
  validates_format_of :address, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i

  attr_accessible :address, :location
end

# Company model

class Company < ActiveRecord::Base
  has_many :email_addresses, :as => :emailable, :dependent => :destroy

  validates_presence_of :company_name

  accepts_nested_attributes_for :email_addresses, :reject_if => proc { |attributes| attributes[:address].blank? }

  attr_accessible :company_name, :background, :email_addresses_attributes
end

# Error log

WARNING: Can't mass-assign protected attributes: address, location

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文