Rails I18n 接受_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("答案")
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 接受_nested_attributes_for 和 error_messages_for的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!