This workshop will be retired on May 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Troubleshooting a Rails Application!
You have completed Troubleshooting a Rails Application!
Preview
How to use a Rails exception log to find the problem.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
For this workshop,
we'll be working on this veterinarian app.
0:00
It's very basic right now,
0:03
just letting you create pet objects
with names and birth dates.
0:04
The first thing I want to show you is
how to make sense of an exception so
0:08
you can fix it.
0:12
We're trying to run this automated test
that loads the form to add a new pet.
0:13
But when the test runs and
0:17
attempts to request a page,
we're getting an exception.
0:18
Let's see if we can figure out the issue
and get our app working again.
0:21
We're seeing this error
in the test output.
0:25
It says we're trying to call a method
named any on an ActiveModel::Errors
0:27
object, but
there's no such method defined.
0:31
There's a helpful suggestion for
the method name.
0:34
Did you mean any?
0:36
Note the question mark there
at the end of the method name.
0:38
And then below that is the most
important part of the error message,
0:41
the stack trace.
0:44
The stack trace is a list of all
the methods in your app that were called
0:45
before reaching the error.
0:49
The most recent method, the one where the
error occurred, will always be at the top.
0:50
Notice that it shows the file name and
line number for the error.
0:55
Since we're working in developer mode,
0:59
we'll see the same exception if we try
to load the pet form in a browser.
1:01
The name of the exception is at the top.
1:05
Below that,
you'll see the same helpful suggestions.
1:07
Unlike the error from the test,
1:10
you'll also see the code
where the error occurred.
1:12
So let's open that file from
the top line of the stack trace,
1:16
app/views/pets/_form.html.erb.
1:21
We know from the stack trace
that the error is on line two.
1:25
It says we called an undefined method
any on an ActiveModel::Errors object.
1:28
That's the object returned by pet.errors.
1:33
The error message also had a suggestion,
did you mean any?, with a question mark.
1:36
Let's try changing the method
name to add a question mark.
1:41
Remember that question marks can be
part of a method name in Ruby, and
1:44
are often used to indicate that
a method returns a true or false value.
1:47
Now let's rerun our test.
1:52
rake test
test/controllers/pets_controller_test.rb.
1:55
And this time the test
completes without any errors.
2:04
Let's also try running our server
again and then reloading our page.
2:07
And the form renders correctly.
2:14
We've fixed our bug.
2:15
So remember, when you're looking
at a Rails stack trace, you
2:18
usually want to focus on the filename and
line number listed in the topmost line.
2:21
That'll usually take you
straight to the error.
2:26
You'll be looking at a lot of
stack traces in your Rails career.
2:28
In fact, you'll see stack traces again in
the very next video as we look at how to
2:31
troubleshoot problems in all
the various parts of a Rails request.
2:35
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up