Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
We'll show you how to troubleshoot problems accessing your database. We'll also show you how to use the Rails Logger as a general debugging tool.
You can read more about the Rails Logger on the Rails Guides site.
Suppose you had the index of
pets open in your browser and
0:00
you clicked the link to open the show
page for one of them in a new tab.
0:03
You do some random browsing and
0:08
then go back to the first tab and
click the delete link for that pet.
0:12
Some more random browsing.
0:18
And then you go back to the show
page tab and click Reload.
0:20
You'll get this error,
ActiveRecord::RecordNotFound.
0:24
What should you do in this case?
0:27
Possibly nothing.
0:29
The record is missing
because it's supposed to be.
0:30
You deleted it, after all.
0:32
In the rare event that something
similar happens in production,
0:34
rather than a RecordNotFound error,
0:37
your users will see a 404 page indicating
they tried to access a nonexistent page.
0:39
If you want finer control
over these errors,
0:45
though, one thing you can do
is to rescue the exception.
0:47
We can go into
app/controllers/pets_controller.rb,
0:50
find the show action, and edit it to add
a rescue clause to rescue the exception.
0:53
So we can say rescue
ActiveRecord::RecordNotFound,
1:00
since that's the exception
we were getting.
1:10
In the event that we get such an error,
we can put up a flash
1:12
message of type notice,
saying we couldn't find that pet.
1:18
And then we can redirect users
back to the index of all pets.
1:28
Save that, and let's try reloading
the page with the missing pet.
1:34
Reload it and we get redirected back
to the index of all pets with a flash
1:40
message, we couldn't find that pet.
1:44
You'll have to decide which is right for
your app,
1:48
explicitly handling load errors or
just letting it show a 404 page.
1:50
The ability to explicitly handle errors
is there if you want it, though.
1:54
Sometimes, RecordNotFound errors
can be more serious, though.
1:58
Suppose we were getting this error every
time we clicked an Edit link for a pet.
2:02
ActiveRecord::RecordNotFound Couldn't
find pet with ID, and nothing.
2:07
And if we look in the stack trace,
we see this is occurring at
2:13
app/controllers/pets_controller.rb line
26 in the edit method.
2:15
Let's try opening the file mentioned in
the stack trace and see what we find.
2:21
We'll open
app/controllers/pets_controller.rb and
2:24
we'll go to line 26.
2:30
The problem seems to be with
that call to Pet.find but
2:32
maybe you can't see what it is just yet.
2:35
Let's take another look at the error.
2:38
It says, couldn't find pet with ID, blank.
2:39
It seems like there should
be an ID number after 'id'=.
2:44
That's a little strange.
2:47
Let's take a look at exactly
what we're passing to Pet.find.
2:49
Rails apps have a logger object which
writes text messages to your terminal
2:53
and to a file.
2:57
All those messages you've seen regarding
GET and POST requests, errors,
2:58
and so on were displayed by the logger.
3:02
And you can display your own
messages using the logger too.
3:05
You can call the logger method within
the code for any Rails controller,
3:08
model, or mailer.
3:12
That'll return a logger object,
3:13
and there are several methods
that you can call on that object.
3:15
All these methods take a string and
add it to the log.
3:19
The difference is how
important that message is.
3:22
Debug messages aren't very important and
3:25
should only be included in the log
when debug mode is turned on.
3:27
Info messages have information
about what the app is doing.
3:31
They're probably safe to omit
from the log in most cases.
3:35
Warn messages indicate
a potential problem.
3:38
They should probably be
included in the log.
3:41
Error messages
are definitely a problem and
3:43
should be included in
the log no matter what.
3:46
And fatal messages indicate a problem so
3:48
serious that the app is
probably about to shut down.
3:50
Okay, so we need to use a log message to
look at the contents of params[:pet].
3:54
Since Rails apps turn debug messages off
by default, even in development mode,
3:59
we're going to call the info method
to add a message to the log.
4:03
So we'll add some code right before
the line where the error occurs.
4:07
We'll call logger to
get the logger object.
4:10
I'll call the info method on it and
I'll pass a string to it.
4:15
The :pet parameter is, and
4:20
I'll interpolate some Ruby
code into the string,
4:22
#{params}, close the interpretation,
4:28
close the string out,
and end the method call.
4:32
Let's save that.
4:38
And now let's refresh our browser page so
4:39
that it runs the pets
controller edit method again.
4:41
That will run the code
that calls logger.info.
4:45
Once that's done, scroll back through
your log and you'll see your message.
4:47
The pet parameter is nothing?
4:52
That can't be right.
4:55
We need to figure out what's going on.
4:56
If you look at the log right before our
log message, you'll see these messages.
4:58
That's the GET request our browser
made to load the edit form.
5:03
See the parameters line there?
5:07
That shows the contents
of the params hash.
5:09
We see an id key, but no pet key.
5:11
Aha, there's our problem.
5:15
We were supposed to use params ID when
calling pet.find, not params pet.
5:17
So let's go back into the pets
controller and fix that.
5:25
We'll remove the log message,
save our work, and reload the page.
5:29
Now that we're using a parameter
that actually exists,
5:34
the pet record is found successfully and
the error is gone.
5:37
The Rails logger can be a useful tool for
5:40
debugging your apps in other situations,
too.
5:42
See the teacher's notes if
you'd like more info on it.
5:45
You need to sign up for Treehouse in order to download course files.
Sign up