Ruby on Rails: how to get error messages from a child resource displayed?

后端 未结 8 1412
谎友^
谎友^ 2020-12-12 16:58

I\'m having a difficult time understanding how to get Rails to show an explicit error message for a child resource that is failing validation when I render an XML template.

8条回答
  •  孤城傲影
    2020-12-12 17:36

    I'm not sure if this is the best (or a correct) answer...i'm still learning, but I found this to work pretty well. I haven't tested it extensively, but it does seem to work with rails4:

    validate do |school|
      school.errors.delete(:students)
      school.students.each do |student|
        next if student.valid?
        school.errors.add(:students, student.errors)
      end
    end
    

提交回复
热议问题