Rails: Validation for a simple_form using has_many relationship (e.g. Person, Phone) -


i'm struggling getting desired validation nested models within simple_form. you'll able see models below person can have many phone's, desired behaviour present edit fields existing numbers plus additional 1 should new number, if new number isn't filled in user it's ignore , not saved in database. want achieve similar email.

when landing on /people/:id/edit page blank field being prematurely validated , producing visible errors on form before submitting. doesn't when visiting /people/:id/new page; i'm assuming because new_record? returns true user model on new page? in reading similar post added on: :save parameter validates on phone model although allowed blank records database, perhaps because isn't relevant when user model saving record?

class person < activerecord::base   belongs_to :company   has_many :phones, :as => :phoneable   has_many :emails, :as => :emailable   has_many :addresses, :as => :addressable   attr_accessible :first_name, :job_title, :last_name, :prefix, :phones_attributes, :emails_attributes, :addresses_attributes, :company_id   accepts_nested_attributes_for :phones, allow_destroy: true, reject_if: proc { |attributes| attributes['number'].blank? }   accepts_nested_attributes_for :emails, allow_destroy: true, reject_if: proc { |attributes| attributes['email'].blank? }   accepts_nested_attributes_for :addresses, allow_destroy: true, reject_if: :all_blank   validates :first_name, :last_name, presence: true    def to_s     "#{first_name} #{last_name}"   end end   class phone < activerecord::base   belongs_to :phoneable, polymorphic: true   attr_accessible :number, :phone_type   validates :number, :phone_type, presence: true, on: :save  # suggested in similar post, allows blank records database.   def to_s     "#{phone_type}: #{number}"   end end 

with both new , edit controller i'm creating new instance of each of these models show on form. @person loaded in controller using load_and_authorize_resource part of cancan.

def new   @person.phones << phone.new   @person.emails << email.new end 

here partial view form:

<%= simple_form_for @person, :html => { :class => 'form-horizontal' } |f| %>   <fieldset id="<%= controller.action_name.capitalize %>_person">     <legend><%= controller.action_name.capitalize %> person</legend>      <%= f.input :prefix %>     <%= f.input :first_name %>     <%= f.input :last_name %>     <%= f.input :job_title %>     <%= f.association :company, :prompt => "select associated company..."  %>      <%= f.simple_fields_for :phones |phone| %>       <%= phone.input :phone_type, :collection => %w(work home mobile fax other), :default => "work" %>       <%= phone.input :number %>     <% end %>      <%= f.simple_fields_for :emails |email| %>       <%= email.input :email_type, :collection => %w(work home other), :default => "work" %>       <%= email.input :email %>     <% end %>      <div class="form-actions">       <%= f.submit nil, :class => 'btn btn-primary' %>       <%= link_to t('.cancel', :default => t("helpers.links.cancel")),                   people_path, :class => 'btn' %>     </div>       </fieldset> <% end %> 

many in advance :-)


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -