0

How do you format columns names in error messages?

class Person
  validates_presence_of :name, :address, :email
  validates_length_of :name, in: 5..30
end

person = Person.create(address: '123 First St.')
person.errors.full_messages
# => ["Name is too short (minimum is 5 characters)",   
"Name can't be blank", "Email can't be blank"]

For example, when there is an error instead I want it to print

Full name can't be blank. (instead of 'Name')

How do I do this since in the model/database its stored as :name.

Is there someway I can link a string to :name?

Mark
  • 1,337
  • 23
  • 34

1 Answers1

3

You can specify a custom message.

validates_length_of :name, in: 5..30, message: 'Full name must be between 5 and 30 characters'

http://guides.rubyonrails.org/active_record_validations.html#message

You can also just translate the attribute

en:
  activerecord:
    attributes:
      person:
        name: "Full name"

By adding that to the config/locales/en.yml file http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models

Mark
  • 1,337
  • 23
  • 34
HarlemSquirrel
  • 8,966
  • 5
  • 34
  • 34