Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

internationalization - Rails I18n accepts_nested_attributes_for and error_messages_for

I've got two models

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"

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"

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As of Rails 3.2.0, the i18n yaml has changed to

en: 
  activerecord:
    attributes:
      survey_response:
        foo: "Foo"
      survey_response/answers:
        answer_text: "Response"

(Note the slash.) This also permits you to define an attribute name on the collection itself, e.g.

en: 
  activerecord:
    attributes:
      survey_response:
        foo: "Foo"
        answers: "Ripostes"
      survey_response/answers:
        answer_text: "Response"

Source: https://github.com/rails/rails/pull/3859


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...