Rail3 attr_accessbile 用于嵌套模型
我很难理解为什么这些属性仍然受到保护。我应该使用父模型中的范围来允许这些属性吗?
# 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论