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