How to add own validation message for accepts_nested_attributes_for?

I have these 2 models:

users

has_one :additional_data, :dependent => :destroy
accepts_nested_attributes_for :additional_data

additional_data

belongs_to :user
validates :skype, :presence => {:message => 'cannot be blank.'}

and form:

<%= form_for@user do |f| %>
  <p>
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </p>
  <%= f.fields_for :additional_data do |ad| %>
        <p>
          <%= ad.label :skype, "Skype" %><br />
          <%= ad.text_field :skype %>
        </p>
  <% end %>
<% end %>

If I send empty form, the message about missing Skype data is not displayed. What am I missing?

Also, I would like to ask you, if I can according value in variable in URL validate the additional_data.

Answers


By default, Rails won't display inline errors.

You can either use a plugin like simple form which I recommend, or display the errors at the beginning of the form by iterating over @user.errors.full_messages.


Check out validates_associated. It validates whether the associated object or objects are all valid themselves.

# user.rb
has_one :additional_data, :dependent => :destroy

validates_associated :additional_data

accepts_nested_attributes_for :additional_data

Need Your Help

how to find part of line java?

java regex

I need some portion of the file to display.Actually by using Matcher and Pattern I found a word in the file .By using Matcher.start() and Matcher.end() I got the postion of the word.But how can i g...

Implementing jaccard similarity in c#

c# similarity

I am trying to understand "Jaccard similarity" between 2 arrays of type double having values greater than zero and less than one.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.