更改轨道模型属性的标签
Change Rail Model attribute's label
这是我的模型:
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
当我在 body 字段上收到验证错误时,我得到 "Body can't be blank",我想在模型中将其更改为 "Content Can't be blank"。如何更改字段标签?
在config/locales/en.yml
中添加这个
en:
activerecord:
attributes:
post:
body: "Content"
这将在给出错误时更改属性名称
您可以在 config/locales/en.yml
中通过覆盖模型的属性标签来做到这一点:
en:
...
activerecord:
attributes:
post:
body: 'Content'
对于 Mongoid
,将上面的 activerecord
替换为 mongoid
。如果这不起作用,请尝试:
en:
mongoid:
attributes:
body: 'Content'
这称为 I18n 的翻译过程。有了这个,您基本上可以覆盖任何模型(例如,用户 --> 客户)、属性、错误消息等的命名等。在它之上,您还可以有其他文件,如 es-CO.yml
,用于目标翻译开箱即用的其他语言。
您可以阅读有关 translation here 的更多信息。
这是我的模型:
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
当我在 body 字段上收到验证错误时,我得到 "Body can't be blank",我想在模型中将其更改为 "Content Can't be blank"。如何更改字段标签?
在config/locales/en.yml
en:
activerecord:
attributes:
post:
body: "Content"
这将在给出错误时更改属性名称
您可以在 config/locales/en.yml
中通过覆盖模型的属性标签来做到这一点:
en:
...
activerecord:
attributes:
post:
body: 'Content'
对于 Mongoid
,将上面的 activerecord
替换为 mongoid
。如果这不起作用,请尝试:
en:
mongoid:
attributes:
body: 'Content'
这称为 I18n 的翻译过程。有了这个,您基本上可以覆盖任何模型(例如,用户 --> 客户)、属性、错误消息等的命名等。在它之上,您还可以有其他文件,如 es-CO.yml
,用于目标翻译开箱即用的其他语言。
您可以阅读有关 translation here 的更多信息。