在 Rails 5 中,belongs_to
默认情况下需要关系,并在每次失败的验证时给出模型必须存在的消息。我想belongs_to
一次本地化每次出现的默认消息。例如 ...
在我的模型中:
class Person < ApplicationRecord
has_many :books
end
class Book < ApplicationRecord
belongs_to :person
validates :author_date, presence: true
end
Run Code Online (Sandbox Code Playgroud)
在我的本地化文件中:
en:
activerecord:
errors:
blank: "can't be blank" # not showing on book.errors[:person]
Run Code Online (Sandbox Code Playgroud)
在控制台中:
book = Book.new
book.save
puts book.errors.to_json
>> {person: ["must exist"], :author_date: ["can't be blank"]}
Run Code Online (Sandbox Code Playgroud)
如何本地化YML 文件中必须存在的消息。