Ruby on rails 在ruby/rails中,如何将多个参数传递给Proc?
以下是给我的问题:Ruby on rails 在ruby/rails中,如何将多个参数传递给Proc?,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-3.2,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 3.2,以下是给我的问题: accepts_nested_attributes_for :photo, :reject_if => proc { |attributes| attributes['image'].blank? }, :reject_if => proc { |attributes| attributes['photo_title'].blank? }, :allow_destroy => true 我想这是因为我在打电话:拒绝两次,不是100%确定。但当我取消对照
accepts_nested_attributes_for :photo,
:reject_if => proc { |attributes| attributes['image'].blank? },
:reject_if => proc { |attributes| attributes['photo_title'].blank? },
:allow_destroy => true
我想这是因为我在打电话:拒绝两次,不是100%确定。但当我取消对照片标题的注释时,如果我选择了一行,则我的图像无法上传。如果我把这行注释掉,它就会注释掉
如何将两个条件合并为一个拒绝条件?如果这有道理的话
亲切问候试试这个
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 => proc { |attributes| attributes['image'].blank? || 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
}
胖箭头参数实际上是一个散列,大括号本质上是由Ruby在背后添加的。散列不允许重复键,因此第二个:如果值覆盖了第一个,则拒绝\u,并且您最终得到以下结果:
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? },
:reject_if => proc { |attributes| attributes['photo_title'].blank? },
:allow_destroy => true
}
胖箭头参数实际上是一个散列,大括号本质上是由Ruby在背后添加的。散列不允许重复键,因此第二个:如果值覆盖了第一个,则拒绝\u,并且您最终得到以下结果:
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
同样的事情正在发生,所以我想还有一个更深层次的问题。我在这两个字段上都设置了:_destroy=>1,所以不确定为什么它不起作用。同样的事情正在发生,所以我想这是一个更深层次的问题。我在这两个字段上都设置了:_destroy=>1,所以不确定它为什么不工作。为什么不使用extern方法来干净地处理逻辑?为什么不使用extern方法来干净地处理逻辑?