本文介绍了accepts_nested_attributes_for忽略空白值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

class Profile
  has_many :favorite_books, :dependent => :destroy
  has_many :favorite_quotes, :dependent => :destroy

  accepts_nested_attributes_for :favorite_books, :allow_destroy => true
  accepts_nested_attributes_for :favorite_quotes, :allow_destroy => true
end

我有一个动态表格,您可以按'+'添加新的文本区域以创建新的收藏夹.我想做的就是忽略空白,我发现在更新控制器中这比非嵌套属性更难排序.

I have a dynamic form where you press '+' to add new textareas for creating new favorites.What i want to do is ignore the blank ones, I find this harder to sort through in the update controller than a non nested attribute.

我暂时拥有的是after_save回调中的黑客,删除了空记录.忽略这些空白对象的最合理的方式是什么?

What i have temporarily is a hack in the after_save callback deleting the empty records. Whats the most rails way to ignore these blank objects?

我不希望验证和错误,只是一个无声的删除/忽略.

I dont want validation and errors, just a silent deletion/ignore.

推荐答案

内置验证:

:reject_if => lambda { |c| c[:name].blank? },

这篇关于accepts_nested_attributes_for忽略空白值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 23:09