1.原始问题：Validation for nested attributes
Iam using nested forms in rails 3.2.8 when i was go in to the edit page when ever you add the nested attributes and make it as a empty and submit the form the nested attributes was not validated and not displaying the message for these how to validates the nested attributes in nested form
Let us consider your code is as 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 one company and suppose we want to accept nested attributes for 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 and address.
You can accept company attributes from user view and on user save it will check for both validates i.e. for user as well as company.
If any validation fails, you will get it in user
For that add following lines to your User view
<% if @user.errors.any? %><% end %>The form contains <%= pluralize(@user.errors.count, "error") %>.
<% @user.errors.full_messages.each do |msg| %>
- <%= msg %><% end %>