排除错误消息中的属性名称 [英] Exclude attribute name prepending in error message

查看:17
本文介绍了排除错误消息中的属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的国际化:

I have a simple internationalization:

attributes:
  user:
    login: Login
errors:
  template:
    header: "Errors"
    body: ""
  models:
    user:
      attributes:
       login:
         taken: "The chosen {{attribute}} is already registered"

产生的错误信息如下:

登录 选择的登录已经注册

Login The chosen Login is already registered

看起来,Rails 会自动在错误消息前面加上属性名称.这迫使我只能以一种方式排列消息 - 将属性名称作为第一个单词.

It seems, like Rails automatically prepends the error messsage with attribute name. This forces me to arrange messages in only one way - with attribute name as a first word.

我需要以下错误信息:

选择的登录已经注册

如何将 Rails 配置为不在错误消息前面加上属性名称?

How can I configure Rails to not prepend the error message with the attribute name?

推荐答案

您需要指定错误消息格式才能删除模型名称.我相信这只有在 Rails 3.2.6 及更高版本中才有可能.有更多详细信息在 Rails 指南中的 I18n 章节.

You need to specify the error message format in order to remove the model name. I believe this is only possible as of Rails 3.2.6 and up. There are more details on the I18n chapter in Rails Guides.

en:
  errors:
    format: "%{message}"

默认为 %{attribute} %{message}.

这篇关于排除错误消息中的属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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