Since in Rails 3 form.error_messages is deprecated I'm using a partial in which I iterate over full_messages and structure my html like this:
<% model.errors.full_messages.each do |msg| %>
<p><%= msg %></p>
<% end %>
However the app's default locale is not English and in my language the structure of the full_messages is kinda unnatural:
{{attribute}} {{message}}
I saw from the source of generate_full_messages that I can localize the format and so in my locale's yml file (bg.yml) I added this:
bg:
activerecord:
errors:
full_messages:
format: "[...]"
However the form开发者_如何转开发at of the validation errors stays the same.
Change your current code
<% @object.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
With this
<% @object.errors.messages.values.each do |msg| %>
<% msg.each do |m| %>
<li><%= m %></li>
<%end %>
<% end %>
And in your model customize the message:
validates :attribute, :presence => { :message => 'Attribute cannot be blank' }
don't know if it can help, but a locale file for Bulgarian is available on Github.
you may also try this (should work according to rails guides):
bg:
errors:
format: "%{message}"
messages: &error_messages
empty: "Something something %{attribute} something something"
this blog post and this stack overflow issue also talk about weird {{attribute}} {{message}}
structures. Seems caused by a conflict between two I18n gems installed on the same server.
Returns all the full error messages for a given attribute in an array.
@object.errors.full_messages_for(:name)
=> ["Name is too short (minimum is 5 characters)", "Name can't be blank"]
精彩评论