ruby on rails - Nested Attributes: unwanted validation despite of reject_if : All_blank -


i new rails advise appreciated.

i have class entry nested attributes addresses,

/app/models/entry.rb

class entry < activerecord::base   has_many :addresses, :dependent => :destroy   accepts_nested_attributes_for :addresses,                                 :allow_destroy => true,                                 :reject_if => :all_blank end 

with class addresses this

/app/models/address.rb

class address < activerecord::base   belongs_to :entry   validates :zip, :presence => true end 

and in nested form have

/app/view/entries/_form.html.slim

= simple_form_for(@entry) |f|   = f.error_notification   - @entry.addresses.build   .form-inputs     = f.simple_fields_for :addresses |address|       = render 'address_form', :f => address 

the idea when form rendered, 'build' create empty 'address' in addition current addresses listed in database. when changes saved, if new address created still empty, rejected , not saved database.

however validation in address.rb doing validation before saving, hence user cannot proceed saving action. there left out?

you might try explicitly naming attributes in address model checked before new, empty 1 created. this:

# in app/models/entry.rb  accepts_nested_attributes_for :addresses, reject_if: lambda {|attributes| nested_address_is_empty?(attributes) }  private  def self.nested_address_is_empty?(attrs)   attrs['line_1'].blank? && attrs['line_2'].blank? && attrs['zip'].blank? end 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -