English 中文(简体)
我如何通过不止一个论点在废墟/铁路进行防御?
原标题:How do I pass more than one argument to Proc in ruby/rails?

以下是我的问题:

accepts_nested_attributes_for :photo, 
:reject_if => proc { |attributes| attributes[ image ].blank? }, 
:reject_if => proc { |attributes| attributes[ photo_title ].blank? },
:allow_destroy => true

我认为,这只是因为我说:如果是两次,而不是100%。 但是,如果我选择一个照相机,照相——标题拒绝——如果我照相,我就没有上载。 如果我这样说的话,它就是这样做的。

我如何把这两种条件结合起来,以拒绝——如果情况如何? 如果是这样的话,是有意义的。

幼儿园

最佳回答

This:

accepts_nested_attributes_for :photo, 
  :reject_if => proc { |attributes| attributes[ image ].blank? }, 
  :reject_if => proc { |attributes| attributes[ photo_title ].blank? },
  :allow_destroy => true

这与这一点相同:

accepts_nested_attributes_for :photo, {
  :reject_if => proc { |attributes| attributes[ image ].blank? }, 
  :reject_if => proc { |attributes| attributes[ photo_title ].blank? },
  :allow_destroy => true
}

粗鲁的论据实际上是哈希语,但鲁比拉在你背后基本上添加了bra。 A Hash don t允许重复钥匙,即第二个<代码>:reject_if 价值超过第一种标准,最后是:

accepts_nested_attributes_for :photo,
  :reject_if => proc { |attributes| attributes[ photo_title ].blank? },
  :allow_destroy => true

你们可以把这两种条件结合起来,以便:

accepts_nested_attributes_for :photo,
  :reject_if => proc { |attributes| attributes[ image ].blank? || attributes[ photo_title ].blank? }, 
  :allow_destroy => true

也可以使用单独的方法:

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
问题回答

引言

accepts_nested_attributes_for :photo, 
 :reject_if => proc { |attributes| attributes[ image ].blank? || attributes[ photo_title ].blank?}, 
 :allow_destroy => true




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签