views:

24

answers:

2

Hi,

Hopefully someone can point me in the right direction. I have a en.yml file

en:
  hello: "Hello world"
  activerecord:
    models:
      recipe: "Recipe"
    attributes:
      recipe:
        title: "Recipe title"
    errors:
      models:
        recipe:
          attributes:
            title:
              blank: "{{attribute}} cannot be left blank"

In my model I validate the presence of the title field

  validates_presence_of :title

However, the error message that I see on the page is something like this

Recipe title Recipe title cannot be left blank.

I cannot figure out why the attribute name is repeated twice.

Any ideas?

+1  A: 

Because rails prefixes the error message with the attribute name. You don't need {{attribute}} in the message. This would work as expected:

en:
  hello: "Hello world"
  activerecord:
    models:
      recipe: "Recipe"
    attributes:
      recipe:
        title: "Recipe title"
    errors:
      models:
        recipe:
          attributes:
            title:
              blank: "cannot be left blank"
Tomas Markauskas
So can I prefix the error message with something like "Recipe {{attribute}} some message"?
iHeartDucks
As far as I know you can't do it like that (at least not for the "blank" message), because the name still would be added at the beginning of the message.
Tomas Markauskas
good to know, Thanks Tomas
iHeartDucks
A: 

There is a fix that makes error messages more customizable (attribute names aren't shown in the beginning of the sentence). You can find it here : http://adamhooper.com/eng/articles/5

arnvald