Rails I18n accepts_nested_attributes_for和error_messages_for [英] Rails I18n accepts_nested_attributes_for and error_messages_for
问题描述
我有两个模型
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
在嵌套形式中,如果验证失败,则会在屏幕上显示此错误:
In my nested form if validation fails I get this error displayed on the screen:
答案答案文本不能为空"
"answers answer text can't be blank"
我已经使用Rails I18n成功地自定义了属性名称.它的行为并不完全符合我的期望.以下yml文件不会影响在error_messages_for
I've customized my attribute names somewhat successfully using rails I18n. It doesn't behave exactly how I would expect though. The yml file below doesn't affect how the attribute name is printed in error_messages_for
en:
activerecord:
models:
survey_response:
answers: "Response"
但是如果从脚本/控制台我尝试
SurveyResponse.human_attribute_name("answers")
But if from script/console I try
SurveyResponse.human_attribute_name("answers")
我得到了响应"的预期结果.
I get the expected result of "Response".
我想做的是让验证错误消息说:
What I'd like to do is have the validation error message say:
响应答案文本不能为空".有什么需要解决的想法吗?
"Response answer text can't be blank". Any ideas what I need to fix?
推荐答案
尝试一下:
en:
activerecord:
models:
survey_response:
answers:
answer_text: "Response"
我正在使用Rails 3,并且对我有用(我的i18n文件有点不同,使用属性"而不是模型.我不知道这在2.3中是否有效)
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"
在此之前,我试图在yml中创建一个名为"answers_answer_text"的属性,但是该属性不起作用.
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.
这篇关于Rails I18n accepts_nested_attributes_for和error_messages_for的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!