如何在rails嵌套表单中销毁空或空白字段

时间:2014-11-26 10:58:26

标签: ruby-on-rails

我如何在rails中使用'reject_if'。并摧毁空白或空白区域

我正在使用此行代码

 accepts_nested_attributes_for :user_twitter_reals,:allow_destroy => true, :reject_if => lambda { |a| a.destroy if a[:twitter_url].blank? }#,reject_if:proc { |user_twitter_reals_attributes| attributes.all? { |key, value| key == '_destroy' || value.blank? } }
  accepts_nested_attributes_for :user_twitter_chipcounts,:allow_destroy => true, :reject_if => lambda { |a| a[:twitter_url].blank? }
  accepts_nested_attributes_for :user_twitter_fakes,:allow_destroy => true, :reject_if => lambda { |a| a[:twitter_url].blank? }
  accepts_nested_attributes_for :user_cells,:allow_destroy => true, :reject_if => lambda { |a| a[:cell_number].blank? }
  accepts_nested_attributes_for :user_facebook_personals,:allow_destroy => true, :reject_if => lambda { |a| a[:facebook_url].blank? } 
  accepts_nested_attributes_for :user_facebook_fansites,:allow_destroy => true, :reject_if => lambda { |a| a[:facebook_url].blank? } 
  accepts_nested_attributes_for :user_facebook_fakes,:allow_destroy => true, :reject_if => lambda { |a| a[:facebook_url].blank? } 
  accepts_nested_attributes_for :user_social_account 
  accepts_nested_attributes_for :user_websitess,:allow_destroy => true, :reject_if => lambda { |a| a[:site_url].blank? } 
  accepts_nested_attributes_for :user_wikipedias,:allow_destroy => true, :reject_if => lambda { |a| a[:wikipidea_url].blank? } 
  accepts_nested_attributes_for :user_imdbs,:allow_destroy => true, :reject_if => lambda { |a| a[:imdburl].blank? } 
  accepts_nested_attributes_for :user_googlepluss,:allow_destroy => true, :reject_if => lambda { |a| a[:gooplus_url].blank? } 
  accepts_nested_attributes_for :user_youtubes,:allow_destroy => true, :reject_if => lambda { |a| a[:youtube_url].blank? } 
  accepts_nested_attributes_for :user_myspaces,:allow_destroy => true, :reject_if => lambda { |a| a[:myspace_url].blank? } 
  accepts_nested_attributes_for :user_linkedins,:allow_destroy => true, :reject_if => lambda { |a| a[:linkedin_url].blank? } 
  accepts_nested_attributes_for :user_pinterests,:allow_destroy => true, :reject_if => lambda { |a| a[:pinterest_url].blank? } 
  accepts_nested_attributes_for :usersetting,:allow_destroy => true

2 个答案:

答案 0 :(得分:0)

尝试

 accepts_nested_attributes_for :user_facebook_personals,:allow_destroy => true, :reject_if => proc { |a| a['facebook_url'].blank? }

为休息做同样的事情

答案 1 :(得分:-1)

accepts_nested_attributes_for :user, :reject_if => proc { |attributes| attributes['password'].blank? }

请对嵌套属性执行相同的操作。

如果所有属性都为空,请将其拒绝,如

accepts_nested_attributes_for :location, :reject_if => :all_blank, :allow_destroy => true

请参阅nested_attributs with proc