1 00:00:00,420 --> 00:00:04,110 Syntax errors are often the easiest problems to find and fix. 2 00:00:04,110 --> 00:00:07,660 Suppose that we get this error when loading the list of pets. 3 00:00:07,660 --> 00:00:11,830 We see SyntaxError in PetsController#index. 4 00:00:11,830 --> 00:00:18,198 We get a file name and a line number, and we see syntax error, unexpected '>'. 5 00:00:18,198 --> 00:00:22,888 The error points us directly to the file and line number where the problem is, 6 00:00:22,888 --> 00:00:25,960 line one of app/models/pet.rb. 7 00:00:25,960 --> 00:00:26,540 In this case, 8 00:00:26,540 --> 00:00:30,291 we even get a hint as to the nature of the problem, unexpected greater than sign. 9 00:00:31,390 --> 00:00:34,470 Let's do a web search to figure out what went wrong. 10 00:00:34,470 --> 00:00:39,025 This line is the start of a class definition, so we'll search for Ruby class 11 00:00:39,025 --> 00:00:43,960 definition. 12 00:00:43,960 --> 00:00:48,934 Skimming the results, we see one that talks about using a less than symbol. 13 00:00:48,934 --> 00:00:51,880 That looks interesting, so let's check that out. 14 00:00:51,880 --> 00:00:55,380 If we scroll down, we see a section on class inheritance. 15 00:00:55,380 --> 00:00:59,800 That's relevant to us because our model class inherits from Application Record. 16 00:00:59,800 --> 00:01:03,080 And in their sample code, it looks like they're using a less than symbol to 17 00:01:03,080 --> 00:01:06,470 indicate a subclass, not a greater than symbol. 18 00:01:06,470 --> 00:01:10,868 So the error is that we're supposed to use a less than symbol to indicate that Pet is 19 00:01:10,868 --> 00:01:14,650 a subclass of ActiveRecord Base, not a greater than symbol. 20 00:01:14,650 --> 00:01:21,364 Let's try changing that, save our work, and reload our page. 21 00:01:21,364 --> 00:01:23,150 It looks like everything works again. 22 00:01:24,390 --> 00:01:27,060 Let's look at another common mistake in code. 23 00:01:27,060 --> 00:01:29,860 Sometimes Ruby will indicate the error occurred on one line but 24 00:01:29,860 --> 00:01:32,480 the real problem is elsewhere in the file. 25 00:01:32,480 --> 00:01:35,320 Suppose we get this error when loading the pets index. 26 00:01:35,320 --> 00:01:38,700 Syntax error in PetsController#index. 27 00:01:38,700 --> 00:01:43,770 It indicates a problem on the pets_controller.rb file line 56. 28 00:01:43,770 --> 00:01:48,590 Let's open that file and line number and see if we can figure out what's going on. 29 00:01:49,700 --> 00:01:54,870 So app/controllers/pets_controller, and we'll scroll down to line 56. 30 00:01:56,380 --> 00:02:01,820 We don't really see a problem here, line 56 is just the end of the file. 31 00:02:01,820 --> 00:02:05,770 But if we scroll higher in the file, suppose we see this. 32 00:02:05,770 --> 00:02:07,580 See something odd there? 33 00:02:07,580 --> 00:02:10,900 There's no end keyword at the end of the new definition. 34 00:02:10,900 --> 00:02:14,812 So Ruby thinks that all the remaining code in the file is part of the new method. 35 00:02:14,812 --> 00:02:18,180 An error doesn't get reported until the end of the file 36 00:02:18,180 --> 00:02:21,250 when Ruby fails to find the expected number of end keywords. 37 00:02:22,580 --> 00:02:29,290 So let's add that end keyword back in, save and reload and our problem is fixed. 38 00:02:30,860 --> 00:02:34,820 So while Ruby and Rails try to guide you directly to the line with the problem, 39 00:02:34,820 --> 00:02:36,660 sometimes that isn't possible. 40 00:02:36,660 --> 00:02:39,400 If this happens, be prepared to widen your search a little bit. 41 00:02:41,010 --> 00:02:43,520 There's one more common issue I'd like to show you. 42 00:02:43,520 --> 00:02:47,770 Sometimes typos in your code cause syntax errors and sometimes they cause undefined 43 00:02:47,770 --> 00:02:52,980 variable or undefined method errors, but you fix them in much the same way. 44 00:02:52,980 --> 00:02:55,730 Suppose we're trying to load the show view for an individual pet, 45 00:02:55,730 --> 00:02:58,830 but we're getting this error. 46 00:02:58,830 --> 00:03:03,616 It says, NameError in Pets#show and it's showing an error in 47 00:03:03,616 --> 00:03:10,180 the show.html.erb template, undefined local variable or method pet. 48 00:03:10,180 --> 00:03:12,550 Here's a potentially helpful error message. 49 00:03:12,550 --> 00:03:16,310 Did you mean @pet, the instance variable? 50 00:03:16,310 --> 00:03:20,650 The problem is that we forgot the at symbol character at the start of the pet 51 00:03:20,650 --> 00:03:21,830 instance variable name. 52 00:03:21,830 --> 00:03:25,820 So Ruby thinks we're trying to access a local variable named pet 53 00:03:25,820 --> 00:03:29,025 rather than an instance variable named @pet. 54 00:03:30,070 --> 00:03:36,828 So if we open up app/views/pets/show.html.erb, 55 00:03:36,828 --> 00:03:41,500 and there on line five, you see the misnamed variable. 56 00:03:41,500 --> 00:03:45,749 If we add an at symbol, save our work, and reload, 57 00:03:45,749 --> 00:03:49,265 everything works. 58 00:03:49,265 --> 00:03:52,355 Thus far, we've focused on fixing error messages, but 59 00:03:52,355 --> 00:03:54,605 what happens when your site just doesn't look right and 60 00:03:54,605 --> 00:03:57,395 there's no error message to tell you where the problem is? 61 00:03:57,395 --> 00:03:58,625 We'll look at that in the next video.