我有两个型号
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( “答案”)
我得到了“响应”的预期结果。
我想要做的是验证错误消息说:
“回复答案文字不能为空”。我需要解决的任何想法?
答案 0 :(得分:54)
从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"
答案 1 :(得分:14)
试试这个:
en:
activerecord:
models:
survey_response:
answers:
answer_text: "Response"
我正在使用Rails 3,这对我有用(我的i18n文件有点不同,使用“属性”而不是模型。我不知道这是否适用于2.3)
en:
activerecord:
attributes:
survey_response:
answers:
answer_text: "Response"
在此之前,我试图在yml中创建一个名为“answers_answer_text”的属性,但它无法正常工作。
我希望这能解决你的问题。