【问题标题】:Rails custom validation messages with namespaced models带有命名空间模型的 Rails 自定义验证消息
【发布时间】:2013-01-17 18:36:28
【问题描述】:

This 是我正在尝试的方向,但我无法让它与命名空间模型一起使用。

class Surveys::Hospital::JobMatch < ActiveRecord::Base
  has_many :job_match_answers,
           :class_name => "Surveys::Hospital::JobMatchAnswer",
           :foreign_key => "surveys_hospital_job_match_id"

  validates_presence_of :job_match_answers
end

我希望用户在错误消息中看到“职位名称”而不是“职位匹配答案”。我尝试在 config/locales/en.yml 中更改它。我尝试了几种组合。

1) 嵌套命名空间

en:
  activerecord:
    attributes:
      surveys:
        hospital:
          job_match:
            job_match_answers: "Job titles"

2) 内联命名空间

en:
  activerecord:
    attributes:
      surveys_hospital_job_match:
        job_match_answers: "Job titles"

3) 没有命名空间

en:
  activerecord:
    attributes:
      job_match:
        job_match_answers: "Job titles"

没有任何作用。如何使用命名空间模型执行此操作?

【问题讨论】:

    标签: ruby-on-rails-3 validation namespaces locale


    【解决方案1】:

    我遇到了同样的问题,首先找到了您的问题,然后将搜索范围扩大到 en.yml 在另一个问题中找到了答案:Changing attributes name in en.yml file is not working

    斜线“/”是命名空间分隔符。

    所以

        en:
          activerecord:
             attributes:
               surveys/hospital/job_match:
                 job_match_answers: "Job titles"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-21
      • 2012-08-12
      • 1970-01-01
      • 2012-04-24
      • 1970-01-01
      • 2011-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多