[英]How do I pass more than one argument to Proc in ruby/rails?
The following is giving me issue: 以下是给我的问题:
accepts_nested_attributes_for :photo,
:reject_if => proc { |attributes| attributes['image'].blank? },
:reject_if => proc { |attributes| attributes['photo_title'].blank? },
:allow_destroy => true
I think it's because I'm calling :reject_if twice, not 100% sure. 我认为这是因为我两次打电话给:reject_if,而不是100%确定。 But when ever I uncomment the photo_title reject_if line my image doesn't get upload if I select one.
但是,每当我取消注释photo_title reject_if行时,如果选择一个,我的图像就不会上传。 If I comment the line out then it does.
如果我注释掉那行,那就可以了。
How can I combine both conditions into one reject_if condition? 如何将两个条件合并为一个reject_if条件? If that makes sense.
如果这样的话。
Kind regards 亲切的问候
This: 这个:
accepts_nested_attributes_for :photo,
:reject_if => proc { |attributes| attributes['image'].blank? },
:reject_if => proc { |attributes| attributes['photo_title'].blank? },
:allow_destroy => true
is the same as this: 与此相同:
accepts_nested_attributes_for :photo, {
:reject_if => proc { |attributes| attributes['image'].blank? },
:reject_if => proc { |attributes| attributes['photo_title'].blank? },
:allow_destroy => true
}
The fat-arrow arguments are actually a Hash, the braces are essentially added by Ruby behind your back. 粗箭头参数实际上是一个Hash,大括号实际上是Ruby在背后添加的。 A Hash doesn't allow duplicate keys so the second
:reject_if
value overwrites the first one and you end up with this: 哈希不允许重复的键,因此第二个
:reject_if
值将覆盖第一个,您最终会得到以下结果:
accepts_nested_attributes_for :photo,
:reject_if => proc { |attributes| attributes['photo_title'].blank? },
:allow_destroy => true
You can combine both conditions in one Proc though: 但是,您可以将两个条件组合在一个Proc中:
accepts_nested_attributes_for :photo,
:reject_if => proc { |attributes| attributes['image'].blank? || attributes['photo_title'].blank? },
:allow_destroy => true
You could also use a separate method: 您还可以使用单独的方法:
accepts_nested_attributes_for :photo,
:reject_if => :not_all_there,
:allow_destroy => true
def not_all_there(attributes)
attributes['image'].blank? || attributes['photo_title'].blank?
end
Try this 尝试这个
accepts_nested_attributes_for :photo,
:reject_if => proc { |attributes| attributes['image'].blank? || attributes['photo_title'].blank?},
:allow_destroy => true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.