3

I'm trying to an activemodel instance with translations. I find that the only way validations work (with another locale) is by duplicating the error message for every field I defined int he 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 have 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' 

Also, I seem to have to do this for every model, instead of just defining a generic way activemodel should translate ALL models. I tried:

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

But that didn't work. I'm obviously missing something... thanks.

sa125
  • 28,121
  • 38
  • 111
  • 153

1 Answers1

4

Try this:

en:
  errors:
    messages:
      blank: "cannot be empty"
ipsum
  • 1,042
  • 7
  • 17