ruby on rails - Validation for nested attributes -
iam using nested forms in rails 3.2.8 when go in edit page when ever add nested attributes , make empty , submit form nested attributes not validated , not displaying message these how validates nested attributes in nested form
let consider code follow :
user model :
class user < activerecord::base has_one :company attr_accessible :first_name, :last_name validates :first_name, :presence => true validates :last_name, :presence => true accepts_nested_attributes_for :company end
user model has 1 company , suppose want accept nested attributes users company.
comapny model :
class company < activerecord::base belongs_to :user attr_accessible :company_name, :address validates :company_name, :presence => true validates :address, :presence => true end
comapny model has company_name , address. can accept company attributes user view , on user save check both validates i.e. user company. if validation fails, in user add following lines user view
<% if @user.errors.any? %> <div id="error_explanation"> <div class="alert alert-error"> form contains <%= pluralize(@user.errors.count, "error") %>. </div> <ul> <% @user.errors.full_messages.each |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %>
Comments
Post a Comment