使用命名空间模型Rails自定义验证消息

时间:2013-01-17 18:36:28

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

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"

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

1 个答案:

答案 0 :(得分:3)

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

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

所以

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