1 00:00:00,430 --> 00:00:04,810 We have a route set up that says GET requests for /pages should go to the pages 2 00:00:04,810 --> 00:00:08,120 controller index method but that controller doesn't exist yet. 3 00:00:08,120 --> 00:00:10,020 So the request is failing. 4 00:00:10,020 --> 00:00:13,210 Let's create a controller to handle requests for page resources. 5 00:00:13,210 --> 00:00:15,690 We can do that using a Rails generator. 6 00:00:15,690 --> 00:00:18,018 Just as there is a scaffold generator and 7 00:00:18,018 --> 00:00:21,793 a migration generator, there's also a controller generator. 8 00:00:21,793 --> 00:00:27,741 So in our terminal, we need to cancel out of the Rails Server if it's running, 9 00:00:27,741 --> 00:00:31,640 and then we type bin/rails generate controller. 10 00:00:31,640 --> 00:00:34,360 And then the name of the controller we want to generate. 11 00:00:34,360 --> 00:00:37,620 The name of the controller should be the plural form of your resource. 12 00:00:37,620 --> 00:00:42,240 So since this is for page resources, the plural is pages. 13 00:00:42,240 --> 00:00:46,920 Whatever you do, don't type controller on the end of the controller name. 14 00:00:46,920 --> 00:00:50,940 If you do, you'll wind up with a file name pages_controller_controller. 15 00:00:50,940 --> 00:00:52,420 And you don't want that. 16 00:00:52,420 --> 00:00:55,830 If you ever make a mistake like that, here's a handy way to undo it. 17 00:00:55,830 --> 00:01:01,670 Run the same command but replace generate with destroy. 18 00:01:03,170 --> 00:01:06,590 That will automatically delete all the misnamed files. 19 00:01:06,590 --> 00:01:09,210 Obviously the destroy subcommand is a little dangerous. 20 00:01:09,210 --> 00:01:13,360 So don't use it if you've manually added code to any of those files. 21 00:01:13,360 --> 00:01:17,260 Now let's rerun our generate command with the correct resource name, pages. 22 00:01:19,070 --> 00:01:23,143 That will automatically generate a pages_controller.rb file in your 23 00:01:23,143 --> 00:01:27,950 app/controllers subdirectory, along with our app's other controllers. 24 00:01:27,950 --> 00:01:32,140 The word controller will automatically be added onto the name for you. 25 00:01:32,140 --> 00:01:36,040 If we look at that file in our editor we see that it just defines an empty class 26 00:01:36,040 --> 00:01:41,050 named PagesController that inherits from another class named ApplicationController. 27 00:01:41,050 --> 00:01:43,700 There aren't any action methods defined yet, but 28 00:01:43,700 --> 00:01:48,330 now that the controller exists, let's try our request again and see what happens. 29 00:01:48,330 --> 00:01:51,750 We'll launch our server with bin/rails s. 30 00:01:53,480 --> 00:01:56,500 Then we'll go back to the browser and reload the page. 31 00:01:56,500 --> 00:02:00,300 The uninitialized constant PagesController error goes away. 32 00:02:00,300 --> 00:02:05,110 Instead we see an unknown action error, the action index could not be found for 33 00:02:05,110 --> 00:02:06,710 PagesController. 34 00:02:06,710 --> 00:02:09,300 Let's add that index action method now. 35 00:02:09,300 --> 00:02:15,990 In your editor, add def index, end and then save your changes. 36 00:02:15,990 --> 00:02:19,620 There's no need to restart Rails, it will load our changes automatically. 37 00:02:19,620 --> 00:02:24,140 Now let's go back to our browser and hit refresh and the route is complete. 38 00:02:24,140 --> 00:02:26,690 Rails finds the index method it was trying to route to, so 39 00:02:26,690 --> 00:02:29,320 the unknown action error goes away. 40 00:02:29,320 --> 00:02:31,055 Now there's yet another error, though. 41 00:02:31,055 --> 00:02:33,850 PagesController#index is missing a template. 42 00:02:33,850 --> 00:02:36,320 Our request is being routed to a controller action, but 43 00:02:36,320 --> 00:02:37,350 we're missing a view. 44 00:02:37,350 --> 00:02:38,510 We'll fix that next.