0
我想在创建期间和更新操作期间使用不同的reject_ifs。基于RAILS 3中的操作的不同reject_ifs(嵌套属性)
我这里附件模型
has_many :attachments
accepts_nested_attributes_for :attachments, :allow_destroy => true, :reject_if => proc { |attributes| attributes['attachment_description'].blank? }
我想具体:reject_if
选项创建行动和更新操作的另一个特定选项...
例如嵌套的属性:
更新中...
如果描述为空白或零,则拒绝:
:reject_if => proc { |attributes| attributes['attachment_description'].blank? }
上创建... 拒绝,如果文件名称为空或零:
:reject_if => proc { |attributes| attributes['attachment_file_name'].blank? }
我不知道还怎么在accepts_nested_attributes_for
块实施:on=> :update
和:on=> :create
任何变通办法不胜感激。谢谢。
编辑:
试图用这种方式,但没有运气。
has_many :attachments, dependent: :destroy
accepts_nested_attributes_for :attachments, :allow_destroy => true, :reject_if => proc { |attributes| attributes['attachment'].blank? }, on: :create
accepts_nested_attributes_for :attachments, :allow_destroy => true, on: :update
它说...
undefined method `key?' for nil:NilClass