简体   繁体   English

结合使用accepts_nested_attributes_for和rejects_if

[英]Using accepts_nested_attributes_for with rejects_if

I'm using paperclip to attach photos to one of my models: 我正在使用回形针将照片附加到我的其中一个模型上:

class User < ActiveRecord::Base
  has_many :photos
  accepts_nested_attributes_for :photos
end

class Photo < ActiveRecord::Base
  belongs_to :user
  has_attached_file :data
end

How can I use reject_if to ignore data fields to which files are not uploaded by users? 如何使用reject_if忽略用户未上传文件的数据字段?

Try: 尝试:

accepts_nested_attributes_for :photos, :reject_if => proc { |attrs| attrs['data'].blank? }

That should effectively ignore any data fields which are left blank by the user. 这应该有效地忽略用户留空的任何数据字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM