17

我有两个模型

class SurveyResponse
  has_many :answers, :class_name => SurveyResponseAnswer.name
  accepts_nested_attributes_for :answers
end

class SurveyResponseAnswer
  belongs_to :survey_response
  validates_presence_of :answer_text
end

在我的嵌套表单中,如果验证失败,我会在屏幕上显示此错误:

“答案答案文本不能为空”

我已经使用 rails I18n 成功地自定义了我的属性名称。它的行为并不完全符合我的预期。下面的 yml 文件不会影响属性名称在 error_messages_for 中的打印方式

en: 
  activerecord:
    models:
      survey_response:
        answers: "Response"

但如果从脚本/控制台我尝试
SurveyResponse.human_attribute_name("answers")

我得到了“响应”的预期结果。

我想做的是让验证错误消息说:

“响应答案文本不能为空”。有什么想法我需要解决吗?

4

2 回答 2

60

从 Rails 3.2.0 开始,i18n yaml 已更改为

en: 
  activerecord:
    attributes:
      survey_response:
        foo: "Foo"
      survey_response/answers:
        answer_text: "Response"

(注意斜线。)这也允许您在集合本身上定义属性名称,例如

en: 
  activerecord:
    attributes:
      survey_response:
        foo: "Foo"
        answers: "Ripostes"
      survey_response/answers:
        answer_text: "Response"

来源:https ://github.com/rails/rails/pull/3859

于 2012-06-20T23:31:36.763 回答
14

Try this:

en: 
  activerecord:
    models:
      survey_response:
        answers:
          answer_text: "Response"

I am using Rails 3 and this is working for me (my i18n file is a bit different, using "attributes" instead of models. I don't know if this works in 2.3)

en: 
  activerecord:
    attributes:
      survey_response:
        answers:
          answer_text: "Response"

Before that I was trying to create a attribute in the yml called "answers_answer_text" but it was not working.

I hope this solves your issue.

于 2010-06-22T16:17:12.893 回答