如何覆盖 ruby-on-rails 中的通用 activerecord 错误消息?
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现 Rails (2.3.8) 遵循以下路线来翻译错误消息(使用 i18n 0.6.0):
另外,不要忘记更改 full_messages 格式,以便它与您的自定义消息相对应。
这是模型“Horse”的示例,它验证属性“name”(不能为空)。
在您的模型(app/models/horse.rb)中:
在您的翻译文件(config/locales/en.yml)中:
下面是我在其中找到此内容的 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):
In your translation file (config/locales/en.yml):
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.
您需要制作自己的 FormBuilder 并更改其翻译键。
You need to make your own FormBuilder and change it's translation key.