How to override generic activerecord error messages in ruby-on-rails?

In my en.yml translation file, I have:

activerecord: errors: template: header: one: "1 error prohibited this {{model}} from being saved" other: "{{count}} errors prohibited this {{model}} from being saved" 

When an activerecord / validation error occurs in my application during registration, an error message appears:

"1 error prevented the saving of this user session"

Is displayed

(where the user_session model is used). I would rather he say something like

"An error occurred preventing you from logging into your account."

How to override general error message with my specific?

+7
override ruby-on-rails activerecord internationalization
source share
2 answers

I found Rails routes (2.3.8) to translate error messages (with i18n 0.6.0): Also, be sure to change the full_messages format to fit your custom messages.

Here is an example with the Horse model, which checks the name attribute (cannot be empty).

In your model (app / models / horse.rb):

 validates_presence_of :name 

In your translation file (config / locales / en.yml):

 en: activerecord: errors: models: horse: attributes: name: blank: "Hey, are you the horse with no name?" full_messages: format: "%{message}" 

Below is a link to the RoR manual page where I found this. There is also a list of messages required for each verification option.

Symbols and default values ​​may change with later versions of Rails and / or i18n.

+12
source share

You need to create your own FormBuilder and change its translation key.

0
source share

All Articles