Rails 3 - How to define ActiveModel translations for multiple attributes?

I am trying to use an activemodel instance with translations. I believe that the only way to validate (with a different locale) is to duplicate the error message for each field that I defined in the model. So for this model:

require 'active_model'
class User 
  include ActiveModel::Validations
  attr_accessor :first_name, :last_name, :email, :phone
  attr_accessor :address, :ssn, :university, :faculty
  attr_accessor :time_left, :gpa

  validates_presence_of :first_name, :last_name, :email, :phone
  validates_presence_of :address, :ssn, :university, :faculty
  validates_presence_of :time_left, :gpa
end

I need to write this yaml:

en:
  activemodel:
    errors:
      models:
        user:
          attributes:
            phone:
              blank: 'cannot be empty' 
            first_name:
              blank: 'cannot be empty' 
            last_name:
              blank: 'cannot be empty' 
            email:
              blank: 'cannot be empty' 
            gpa:
              blank: 'cannot be empty' 
            address:
              blank: 'cannot be empty' 
            faculty:
              blank: 'cannot be empty' 
            university:
              blank: 'cannot be empty' 

In addition, I seem to have to do this for each model, instead of just defining the general way activemodel should translate ALL models. I tried:

en:
  activemodel:
    errors:
      models:
        attributes:
           blank: 'cannot be empty' 

But that did not work. I am clearly missing something ... thanks.

+3
source share
1 answer

Try the following:

en:
  errors:
    messages:
      blank: "cannot be empty"
+4

Source: https://habr.com/ru/post/1769983/


All Articles