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
end
end
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"
end
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
@school.add_student(params[:bad_email])
respond_to do |format|
if @school.save
# some code
else
format.xml { render :xml => @school.errors, :status => :unprocessable_entity }
end
end
end
end
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"?>
<errors>
<error>Students is invalid</error>
</errors>
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?
Solution 1:
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}")
end
end
end
end
Now @school.errors
will contain the correct errors:
format.xml { render :xml => @school.errors, :status => :unprocessable_entity }
Note:
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}"
Solution 2:
Update Rails 5.0.1
You can use Active Record Autosave Association
class School < ActiveRecord::Base
has_many :students, autosave: true
validates_associated :students
end
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"
end
@school = School.new
@school.build_student(email: 'xyz')
@school.save
@school.errors.full_messages ==> ['You must supply a valid email']
reference: http://api.rubyonrails.org/classes/ActiveRecord/AutosaveAssociation.html
Solution 3:
This is not a public API yet, but Rails 5 stable seems to have ActiveModel::Errors#copy!
to merge errors
between two models.
user = User.new(name: "foo", email: nil)
other = User.new(name: nil, email:"[email protected]")
user.errors.copy!(other.errors)
user.full_messages #=> [ "name is blank", "email is blank" ]
Again, this is not officially published yet (I accidentally find this one before monkey-patching Errors
class), and I'm not sure it will be.
So it's up to you.