Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 full error messages format

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 format of the validation errors stays the same.

like image 708
zbrox Avatar asked Dec 24 '10 18:12

zbrox


3 Answers

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"]

like image 107
Jigar Bhatt Avatar answered Nov 09 '22 07:11

Jigar Bhatt


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' }
like image 24
Erick Eduardo Garcia Avatar answered Nov 09 '22 07:11

Erick Eduardo Garcia


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.

like image 8
m_x Avatar answered Nov 09 '22 06:11

m_x