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

发布于 2024-08-26 06:43:22 字数 501 浏览 6 评论 0原文

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

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

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

“1 个错误禁止保存此用户会话”

显示 (其中 user_session 是正在使用的模型)。我宁愿让它说类似的话

“发生错误,导致您无法登录帐户”。

如何用我的特定错误消息覆盖一般错误消息?

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"  

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

"1 error prohibited this user session from being saved"

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九八野马 2024-09-02 06:43:22

我发现 Rails (2.3.8) 遵循以下路线来翻译错误消息(使用 i18n 0.6.0):
另外,不要忘记更改 full_messages 格式,以便它与您的自定义消息相对应。

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

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

validates_presence_of :name

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

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

下面是我在其中找到此内容的 RoR 指南页面的链接。还有一个列表,其中列出了每种验证变体所需的消息。

符号和默认值可能会随着 Rails 的更高版本和/或国际化。

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).

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

validates_presence_of :name

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.

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

抹茶夏天i‖ 2024-09-02 06:43:22

您需要制作自己的 FormBuilder 并更改其翻译键。

You need to make your own FormBuilder and change it's translation key.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文