加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 04:27:25 所属栏目:百科 来源:网络整理
导读:在我的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 / v
在我的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 error prohibited this user session from being saved”

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

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

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

解决方法

我发现路由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-guides页面的链接.还列出了每种验证变体都需要哪些消息.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读