【问题标题】:The attribute name is repeated twice in the error message属性名称在错误消息中重复了两次
【发布时间】:2010-02-13 20:41:41
【问题描述】:

希望有人能指出我正确的方向。我有一个 en.yml 文件

en:
  hello: "Hello world"
  activerecord:
    models:
      recipe: "Recipe"
    attributes:
      recipe:
        title: "Recipe title"
    errors:
      models:
        recipe:
          attributes:
            title:
              blank: "{{attribute}} cannot be left blank"

在我的模型中,我验证了标题字段的存在

  validates_presence_of :title

但是,我在页面上看到的错误消息是这样的

Recipe title Recipe title cannot be left blank.

我无法弄清楚为什么属性名称重复了两次。

有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails internationalization


    【解决方案1】:

    因为 rails 会在错误消息前加上属性名称。消息中不需要{{attribute}}。这将按预期工作:

    en:
      hello: "Hello world"
      activerecord:
        models:
          recipe: "Recipe"
        attributes:
          recipe:
            title: "Recipe title"
        errors:
          models:
            recipe:
              attributes:
                title:
                  blank: "cannot be left blank"
    

    【讨论】:

    • 那么我可以在错误消息前加上“Recipe {{attribute}} some message”之类的前缀吗?
    • 据我所知,您不能那样做(至少对于“空白”消息而言不能),因为名称仍会添加到消息的开头。
    【解决方案2】:

    有一个修复程序可以使错误消息更加可定制(属性名称未显示在句子的开头)。你可以在这里找到它:http://adamhooper.com/eng/articles/5

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-09
      相关资源
      最近更新 更多