Mik*_*ike 17 ruby-on-rails internationalization 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
Run Code Online (Sandbox Code Playgroud)
在我的嵌套表单中,如果验证失败,我会在屏幕上显示此错误:
"答案答案文字不能为空"
我使用rails I18n成功地定制了我的属性名称.它的行为并不像我期望的那样.下面的yml文件不会影响error_messages_for中打印属性名称的方式
en:
activerecord:
models:
survey_response:
answers: "Response"
Run Code Online (Sandbox Code Playgroud)
但是如果从脚本/控制台我尝试
SurveyResponse.human_attribute_name("答案")
我得到了"响应"的预期结果.
我想要做的是验证错误消息说:
"响应答案文本不能为空".我需要解决的任何想法?
小智 55
从Rails 3.2.0开始,i18n yaml已经改为
en:
activerecord:
attributes:
survey_response:
foo: "Foo"
survey_response/answers:
answer_text: "Response"
Run Code Online (Sandbox Code Playgroud)
(注意斜杠.)这也允许您在集合本身上定义属性名称,例如
en:
activerecord:
attributes:
survey_response:
foo: "Foo"
answers: "Ripostes"
survey_response/answers:
answer_text: "Response"
Run Code Online (Sandbox Code Playgroud)
资料来源:https://github.com/rails/rails/pull/3859
小智 14
试试这个:
en:
activerecord:
models:
survey_response:
answers:
answer_text: "Response"
Run Code Online (Sandbox Code Playgroud)
我正在使用Rails 3,这对我有用(我的i18n文件有点不同,使用"属性"而不是模型.我不知道这是否适用于2.3)
en:
activerecord:
attributes:
survey_response:
answers:
answer_text: "Response"
Run Code Online (Sandbox Code Playgroud)
在此之前,我试图在yml中创建一个名为"answers_answer_text"的属性,但它无法正常工作.
我希望这能解决你的问题.
| 归档时间: |
|
| 查看次数: |
6918 次 |
| 最近记录: |