randombits randombits - 1 year ago 103
reST (reStructuredText) Question

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

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. Hypothetically, I have the following classes:

class School < ActiveRecord::Base
has_many :students
validates_associated :students

def self.add_student(bad_email)
s = Student.new(bad_email)
students << s

class Student < ActiveRecord::Base
belongs_to :school
validates_format_of :email,
:with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i,
:message => "You must supply a valid email"

Now, in the controller, let's say we want to build a trivial API to allow us to add a new School with a student in it (again, I said, it's a terrible example, but plays its role for the purpose of the question)

class SchoolsController < ApplicationController
def create
@school = School.new
respond_to do |format|
if @school.save
# some code
format.xml { render :xml => @school.errors, :status => :unprocessable_entity }

Now the validation is working just fine, things die because the email doesn't match the regex that's set in the validates_format_of method in the Student class. However the output I get is the following:

<?xml version="1.0" encoding="UTF-8"?>
<error>Students is invalid</error>

I want the more meaningful error message that I set above with validates_format_of to show up. Meaning, I want it to say:

<error>You must supply a valid email</error>

What am I doing wrong for that not to show up?

Answer Source

Add a validation block in the School model to merge the errors:

class School < ActiveRecord::Base
  has_many :students

  validate do |school|
    school.students.each do |student|
      next if student.valid?
      student.errors.full_messages.each do |msg|
        # you can customize the error message here:
        errors.add_to_base("Student Error: #{msg}")


Now @school.errors will contain the correct errors:

format.xml  { render :xml => @school.errors, :status => :unprocessable_entity }


You don't need a separate method for adding a new student to school, use the following syntax:

school.students.build(:email => email)

Update for Rails 3.0+

errors.add_to_base has been dropped from Rails 3.0 and above and should be replaced with:

errors[:base] << "Student Error: #{msg}"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download