【问题标题】:How can I use I18N translations with ActiveModel::Validations outside of ActiveRecord?如何在 ActiveRecord 之外将 I18N 翻译与 ActiveModel::Validations 一起使用?
【发布时间】:2017-04-21 16:53:04
【问题描述】:

Rails 指南提供的 I18n 范围特定于在 ActiveRecord 对象中使用 ActiveModel::Validations。例如:

en:
  activerecord:
    errors:
      models:
        some_model:
          attributes:
            name:
              blank: "Please enter your full legal name."

当以这种方式使用ActiveModel::Validations 时,这将不起作用:

class SomeModel
  include ActiveModel::Validations
  validates :name, presence: true
end

改为使用框架默认的“不能为空白”。

如何解决?

【问题讨论】:

    标签: ruby-on-rails ruby rails-i18n activemodel


    【解决方案1】:

    activemodel 替换为activerecord 可以解决此问题并允许所有后续范围工作。示例:

    en:
      activemodel: # <---
        errors:
          models:
            message:
              attributes:
                name:
                  blank: "Please enter your name."
    

    【讨论】:

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