【问题标题】:Customizing header of error_messages_for自定义 error_messages_for 的标头
【发布时间】:2011-04-04 11:59:10
【问题描述】:

我正在尝试使用 error_messages_for 获取自定义错误消息。我正在使用 rails 3 并已下载并安装了插件。 在我看来,我有:

error_messages_for :model

哪个有效并给我错误消息。但是我想更改默认值

1 error prohibited this model from being saved

等等

我已经设法使用本地文件更改了属性名称:

en:
  activemodel:    
    attributes:
      model:
        dateOfBirth: 'Date of birth'
        etc

但是当我尝试添加时:

activemodel:
    errors:
      template:
        header:
          one:    "{{model}} is not valid"
          other:  "There are {{count}} errors prohibiting this {{model}} from being saved"
        # The variable :count is also available
        body: "The following fields have problems:"

正如rails guide 中所建议的那样,它不会更改标题,我也不会收到新的错误消息。

activemodel 和 activerecord 我都试过了。

非常感谢任何帮助

【问题讨论】:

  • 这里已经回答了这个问题:stackoverflow.com/questions/125512/…
  • 也许,但这只是一个静态标题消息,我​​不能做复数等。另外,我不能修改“以下字段存在问题”。此外,我不必在每次包含帮助程序时都设置它。 Rails 指南建议(我认为最好)使用语言环境
  • 你不应该对重复的问题投反对票

标签: ruby-on-rails ruby-on-rails-3 internationalization


【解决方案1】:

我认为问题与我正在使用的插件有关。 Dynamic Form 与 rails 2.3.x 处理错误的方式并不完全相同。因为错误不在 Active Record 的范围内。查看此插件附带的语言环境文件:

en:
  errors:
    template:
      header:
        one:    "1 error prohibited this %{model} from being saved"
        other:  "%{count} errors prohibited this %{model} from being saved"
      body: "There were problems with the following fields:"

如果我编辑这个文件,那么它就可以工作。 很快就会看看这是否可以集成到插件中,使其与 2.3.x 一样工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 2018-12-18
    • 2016-04-05
    • 2011-01-04
    • 2017-02-12
    • 1970-01-01
    相关资源
    最近更新 更多