如何在ruby-on-rails中覆盖通用的activerecord错误消息?

Dav*_*ith 7 activerecord overriding ruby-on-rails internationalization

在我的en.yml翻译文件中,我有:

activerecord:
  errors: 
    template: 
       header: 
         one: "1 error prohibited this {{model}} from being saved"
         other: "{{count}} errors prohibited this {{model}} from being saved"  
Run Code Online (Sandbox Code Playgroud)

在登录我的应用程序期间发生activerecord/validation错误时,错误消息:

"1错误禁止此用户会话被保存"

显示(其中user_session是正在使用的模型).我宁愿让它说出像

"发生了一次错误,导致您无法登录自己的帐户".

如何使用我的特定错误消息覆盖常规错误消息?

Jus*_*ijn 12

我发现路由Rails(2.3.8)跟随转换错误消息(使用i18n 0.6.0):另外,不要忘记更改full_messages格式,使其与您的自定义消息相对应.

这是模型"Horse"的示例,它验证属性"name"(不能为空).

在你的模型中(app/models/horse.rb):

validates_presence_of :name
Run Code Online (Sandbox Code Playgroud)

在您的翻译文件(config/locales/en.yml)中:

en:
  activerecord:
    errors:
      models:
        horse:
          attributes:
            name:
              blank: "Hey, are you the horse with no name?"
      full_messages:
        format: "%{message}"
Run Code Online (Sandbox Code Playgroud)

下面是我发现这个的RoR指南页面的链接.还列出了每种验证变体都需要哪些消息.

符号和默认值可能会随着Rails和/或i18n的更高版本而改变.