0

This is my model:

class Post
  include Mongoid::Document
  include Mongoid::Timestamps

  belongs_to :user, optional: true
  has_many :comments

  field :title, type: String
  field :body, type: String

  validates :title, presence: true
  validates :body, presence: true

end

When I get a validation error on the body field I get "Body can't be blank" and I want to change this to "Content Can't be blank" in the model. How do I change the field label?

Simon Tsang
  • 127
  • 1
  • 3
  • 8

2 Answers2

0

Add this in config/locales/en.yml

en:
  activerecord:
    attributes:
      post:
        body: "Content"

This will change the attribute name while giving errors

Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
0

You can do that in config/locales/en.yml, by overriding the attributes' label of a model:

en:
...
  activerecord:
    attributes:
      post:
        body: 'Content'

For Mongoid, replace activerecord above with mongoid. If that doesn't work try:

en:
  mongoid:
    attributes:
      body: 'Content'

This is called as translation process for I18n. With this, you can basically override the naming etc for any model (say, User --> Customer), attributes, error messages etc. On top of it, you can also have other files like es-CO.yml, to target translation on other languages out-of-the-box.

You can read more about translation here.

kiddorails
  • 12,961
  • 2
  • 32
  • 41
  • Replace `activerecord` above with `mongoid`. – kiddorails Sep 01 '16 at 11:53
  • @kiddorails Shouldn't there be a level `post` between `attributes` and `body` as in the first example on active record? Or would this change all body attributes in all models? – Markus Graf Sep 06 '16 at 06:46