在红宝石上轨我如何重写普通的ActiveRecord的错误信息? [英] How do I override generic activerecord error messages in ruby-on-rails?

查看:127
本文介绍了在红宝石上轨我如何重写普通的ActiveRecord的错误信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my en.yml translation file, I have:

activerecord:
  errors: 
    template: 
       header: 
         one: "1 error prohibited this {{model}} from being saved"
         other: "{{count}} errors prohibited this {{model}} from being saved"  

当一个ActiveRecord /验证错误登录到我的应用程序,该错误信息过程中出现:

When an activerecord/validation error occurs during logging into my application, the error message:

1错误被禁止保存该用户的会话

"1 error prohibited this user session from being saved"

时,显示(其中USER_SESSION正在使用的模型)。我宁愿它这样说:

is displayed (where user_session is the model being used). I'd rather have it say something like

错误已经无法登录到您的帐户发生于prevent你。

"An error has occured to prevent you from logging into your account".

我如何重写的一般错误消息,我的具体吗?

How do I override the generic error message with my specific one?

推荐答案

我找到路线的Rails(2.3.8)遵循翻译的错误消息(与国际化0.6.0): 另外,不要忘记改变full_messages格式,使之与您的自定义消息对应。

I found the routes Rails (2.3.8) follows to translate error messages (with i18n 0.6.0): Also, do not forget to change the full_messages format so that it corresponds with your custom messages.

下面是与模型马,验证属性名(不能为空)的一个例子。

Here's an example with model "Horse", which validates the attribute "name" (cannot be blank).

在你的模型(应用程序/模型/ horse.rb):

In your model(app/models/horse.rb):

validates_presence_of :name

在你的翻译文件(配置/区域设置/ en.yml):

In your translation file (config/locales/en.yml):

en:
  activerecord:
    errors:
      models:
        horse:
          attributes:
            name:
              blank: "Hey, are you the horse with no name?"
      full_messages:
        format: "%{message}"

下面是一个链接到回报率,引导页,在这里我发现了这一点。还有其中所需的验证的每一变体的消息的明细表。

Below is a link to the RoR-guides page where I found this. There's also a list of which messages are required for every variant of validation.

  • ruby on rails guides example and explanation
  • Table with all validations and corresponding messages

的符号和默认设置可能更改与更高版本的Rails和/或国际化的。

The notation and defaults may change with later versions of Rails and/or i18n.

这篇关于在红宝石上轨我如何重写普通的ActiveRecord的错误信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆