【问题标题】:Rails 3 Customer Error messageRails 3 客户错误消息
【发布时间】:2011-05-21 07:33:45
【问题描述】:

我正在尝试做一些非常简单的事情。我在 rails 3 中有一个 validates 存在:

validates_presence_of [:first_nm]

当它触发时,它会给出以下糟糕的消息:

"第一个 nm 不能为空"

我想覆盖错误消息,为“first nm”提供一个友好的字段名称

“请填写您的名字”

我看过各种各样的插件、本地化、人性化属性的教程,但这些似乎都不起作用或者已经过时了。在 Rails 3 中没有简单的方法可以做到这一点吗?

对于本地化,我试过这个:

# Sample localization file for English. Add more files in this directory for other locales.
# See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.

en:
  activerecord:
    errors:
      messages:
        taken: "has already been taken"
        record_invalid: "Validation failed: %{errors}"
        models:
            customer:
              blank: "This is a custom blank message for %{model}: %{attribute}"
              attributes:
                first_nm:
                  blank: "This is a custom blank message for first name"

唉,运气不好。我的错误信息没有改变。

关于可能相关的事情。我不是从 ActiveRecord 继承的,因为这个对象是通过肥皂保存的,而不是数据库。相反,我有以下内容:

class Customer
  extend ActiveModel::Naming
  include ActiveModel::Conversion
  include ActiveModel::Validations


end

【问题讨论】:

    标签: ruby-on-rails validation


    【解决方案1】:

    您在ActiveRecord localization 上看过这个页面吗?它似乎适用于 Rails 3。我现在无法测试它,但从文档看来你可以做到:

    models.user.attributes.first_nm.blank = "Please fill out your First Name"

    【讨论】:

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