简体   繁体   English

如何在ruby / rails中将多个参数传递给Proc?

[英]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.

相关问题 如何在Rails的ruby中的一个控制器中执行多个更新 - how to do more than one updates in one controller in ruby on rails 如何在 Rails 3 的 build_association 调用中传递多个构造函数参数? - How to pass more than one constructor argument in a build_association call in Rails 3? 如果您的ruby on rails脚本中有多个错误,那么它如何知道显示哪个错误? - If you have more than 1 error in your ruby on rails script, how does it know which one do display? 如何将多个参数传递给backgroundrb的工人 - How to pass more than one argument to a worker at backgroundrb 如何在Ruby on Rails中创建多个索引页面 - How to create more than one index page in Ruby on Rails 如何使用Ruby on Rails上的Join将两个或多个参数传递给两个不同的表 - How do i pass two or more parameters to two different tables using Joins on Ruby on rails Ruby on Rails查询多个字段 - Ruby on Rails query more than one fields 如何传递多个参数以在 Rails 中渲染? - How to pass more than one parameters to render in rails? 如何在我指定控制器和操作的link_to调用中传递多个参数? - How do I pass more than one parameter into a link_to call where I specify controller and action? 在 ruby​​ on rails 中,我可以在一个控制器中添加多个资源吗? - In ruby on rails, can I add more than one resources in one controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM