相关疑难解决方法(0)

Rails I18n accepted_nested_attributes_for和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("答案")

我得到了"响应"的预期结果.

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

"响应答案文本不能为空".我需要解决的任何想法?

ruby-on-rails internationalization error-messages-for

17
推荐指数
2
解决办法
6918
查看次数