此:
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
}
脂肪箭頭論點實際上是一個哈希,大括號是必不可少的由Ruby背後加上。 A散列不允許重複鍵,以便第二:reject_if
值覆蓋第一個和你結束了這一點:
accepts_nested_attributes_for :photo,
:reject_if => proc { |attributes| attributes['photo_title'].blank? },
:allow_destroy => true
您可以在一個PROC雖然合併兩個條件:
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
爲什麼不使用extern方法來清理處理邏輯? – apneadiving