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
Sometimes errors are caused by a typo in your code.
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
Syntax errors are often the easiest
problems to find and fix.
0:00
Suppose that we get this error
when loading the list of pets.
0:04
We see SyntaxError in
PetsController#index.
0:07
We get a file name and a line number,
and we see syntax error, unexpected '>'.
0:11
The error points us directly to the file
and line number where the problem is,
0:18
line one of app/models/pet.rb.
0:22
In this case,
0:25
we even get a hint as to the nature of
the problem, unexpected greater than sign.
0:26
Let's do a web search to
figure out what went wrong.
0:31
This line is the start of a class
definition, so we'll search for Ruby class
0:34
definition.
0:39
Skimming the results, we see one that
talks about using a less than symbol.
0:43
That looks interesting,
so let's check that out.
0:48
If we scroll down,
we see a section on class inheritance.
0:51
That's relevant to us because our model
class inherits from Application Record.
0:55
And in their sample code, it looks like
they're using a less than symbol to
0:59
indicate a subclass,
not a greater than symbol.
1:03
So the error is that we're supposed to use
a less than symbol to indicate that Pet is
1:06
a subclass of ActiveRecord Base,
not a greater than symbol.
1:10
Let's try changing that,
save our work, and reload our page.
1:14
It looks like everything works again.
1:21
Let's look at another
common mistake in code.
1:24
Sometimes Ruby will indicate
the error occurred on one line but
1:27
the real problem is elsewhere in the file.
1:29
Suppose we get this error
when loading the pets index.
1:32
Syntax error in PetsController#index.
1:35
It indicates a problem on
the pets_controller.rb file line 56.
1:38
Let's open that file and line number and
see if we can figure out what's going on.
1:43
So app/controllers/pets_controller,
and we'll scroll down to line 56.
1:49
We don't really see a problem here,
line 56 is just the end of the file.
1:56
But if we scroll higher in the file,
suppose we see this.
2:01
See something odd there?
2:05
There's no end keyword at
the end of the new definition.
2:07
So Ruby thinks that all the remaining code
in the file is part of the new method.
2:10
An error doesn't get reported
until the end of the file
2:14
when Ruby fails to find the expected
number of end keywords.
2:18
So let's add that end keyword back in,
save and reload and our problem is fixed.
2:22
So while Ruby and Rails try to guide you
directly to the line with the problem,
2:30
sometimes that isn't possible.
2:34
If this happens, be prepared to
widen your search a little bit.
2:36
There's one more common
issue I'd like to show you.
2:41
Sometimes typos in your code cause syntax
errors and sometimes they cause undefined
2:43
variable or undefined method errors,
but you fix them in much the same way.
2:47
Suppose we're trying to load
the show view for an individual pet,
2:52
but we're getting this error.
2:55
It says, NameError in Pets#show and
it's showing an error in
2:58
the show.html.erb template,
undefined local variable or method pet.
3:03
Here's a potentially
helpful error message.
3:10
Did you mean @pet, the instance variable?
3:12
The problem is that we forgot the at
symbol character at the start of the pet
3:16
instance variable name.
3:20
So Ruby thinks we're trying to
access a local variable named pet
3:21
rather than an instance
variable named @pet.
3:25
So if we open up
app/views/pets/show.html.erb,
3:30
and there on line five,
you see the misnamed variable.
3:36
If we add an at symbol,
save our work, and reload,
3:41
everything works.
3:45
Thus far, we've focused on
fixing error messages, but
3:49
what happens when your site
just doesn't look right and
3:52
there's no error message to
tell you where the problem is?
3:54
We'll look at that in the next video.
3:57
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