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

后端 未结 8 1398
谎友^
谎友^ 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 18:01

    You should use following in the rhtml.

    <%= error_messages_for :school, :student %>
    

    To skip "Students is invalid" message use following in the student.rb

      def after_validation
        # Skip errors that won't be useful to the end user
        filtered_errors = self.errors.reject{ |err| %w{ student}.include?(err.first) }
        self.errors.clear
        filtered_errors.each { |err| self.errors.add(*err) }
      end
    

    EDITED

    Sorry after_validation must be in a school.rb
    

提交回复
热议问题