Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
741 views
in Technique[技术] by (71.8m points)

ruby on rails - accepts_nested_attributes_for ignore blank values

i have

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is a built-in validation:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...