1 00:00:00,820 --> 00:00:04,110 A given Rails app is likely to have a whole bunch of resources, 2 00:00:04,110 --> 00:00:07,315 each with its own controller to handle requests for them. 3 00:00:07,315 --> 00:00:10,260 In our Blog app requests to create, read, update and 4 00:00:10,260 --> 00:00:13,110 delete posts are handled by the PostController. 5 00:00:13,110 --> 00:00:17,550 Now we need to add a PagesController to handle current requests for pages. 6 00:00:17,550 --> 00:00:20,200 But how will the requests get to the PagesController? 7 00:00:20,200 --> 00:00:24,270 How do we tell a request for a post apart from a request for a page? 8 00:00:24,270 --> 00:00:26,248 Rails lets you set up routes for requests. 9 00:00:26,248 --> 00:00:29,894 So that you can send a particular request to a controller that can handle it. 10 00:00:29,894 --> 00:00:31,880 We'll look at how to set up routes next. 11 00:00:33,210 --> 00:00:37,090 We're here in our Blog app viewing the /post path. 12 00:00:37,090 --> 00:00:40,120 It displays a list of all our post model objects. 13 00:00:40,120 --> 00:00:44,370 In this stage, we're gonna set up a list of all our page model objects. 14 00:00:44,370 --> 00:00:48,160 We're going to want that list to be available at the /pages path. 15 00:00:48,160 --> 00:00:50,778 But let's see what happens if we try to visit that path now. 16 00:00:50,778 --> 00:00:56,393 We'll change /post here in the URL bar to /pages. 17 00:00:59,056 --> 00:01:00,270 We see an error. 18 00:01:00,270 --> 00:01:03,210 No route matches get /pages. 19 00:01:03,210 --> 00:01:05,840 So what's this route it's referring to? 20 00:01:05,840 --> 00:01:09,950 Your Rails app has several controllers, each with several actions methods. 21 00:01:09,950 --> 00:01:12,446 When Rails receives a request from a browser, 22 00:01:12,446 --> 00:01:15,390 it needs to know which controller action to send it to. 23 00:01:15,390 --> 00:01:20,330 To determine that, it looks at the http request type and the path. 24 00:01:20,330 --> 00:01:23,830 Remember, that's the part of the URL following the server and port. 25 00:01:23,830 --> 00:01:26,850 Then Rails looks for a route to determine which controller and 26 00:01:26,850 --> 00:01:29,360 which method should handle that request. 27 00:01:29,360 --> 00:01:32,060 So you might have a route that says, get requests for 28 00:01:32,060 --> 00:01:37,460 the /pages path should be sent to the PagesController's index method. 29 00:01:37,460 --> 00:01:40,830 Whenever Rails receives a get request for /pages, 30 00:01:40,830 --> 00:01:44,400 it'll call the index method on the PagesController. 31 00:01:44,400 --> 00:01:47,617 You can get a list of all the routes in a Rails app from your Terminal. 32 00:01:47,617 --> 00:01:52,257 Stop the server if it's running, 33 00:01:52,257 --> 00:01:56,270 and type bin/rails routes. 34 00:01:56,270 --> 00:02:01,740 Windows users, don't forget to type ruby bin\rails instead of bin/rails. 35 00:02:01,740 --> 00:02:05,540 There are lots of routes for post resources but none for page resources. 36 00:02:05,540 --> 00:02:07,460 We're going to need to have one. 37 00:02:07,460 --> 00:02:11,256 A Rails application's routes are configured within the config directory. 38 00:02:11,256 --> 00:02:15,190 In the routes.rb file. 39 00:02:15,190 --> 00:02:18,470 This file contains Ruby code that sets up the routes. 40 00:02:18,470 --> 00:02:20,530 The syntax can be a little confusing, 41 00:02:20,530 --> 00:02:24,240 especially if you're not used to keyword arguments on Ruby methods. 42 00:02:24,240 --> 00:02:26,870 Don't worry about fully understanding it right now. 43 00:02:26,870 --> 00:02:30,250 If you copy the examples we show you here you'll be able to modify them 44 00:02:30,250 --> 00:02:31,610 to suit your needs. 45 00:02:31,610 --> 00:02:34,590 Let's add a route for the /pages path now. 46 00:02:34,590 --> 00:02:39,966 Remember, there are several types of HTTP requests, such as get and post. 47 00:02:39,966 --> 00:02:43,770 When your browser sends a request to view a page, it's a get request. 48 00:02:43,770 --> 00:02:46,030 So we'll call the get method to set up a get route. 49 00:02:47,090 --> 00:02:48,770 As an argument to the get method, 50 00:02:48,770 --> 00:02:52,200 we need to provide the path that this route should match as a string. 51 00:02:52,200 --> 00:02:55,210 So we'll start a string and type /pages. 52 00:02:55,210 --> 00:02:57,380 The next argument to the get method is a hash, 53 00:02:57,380 --> 00:03:01,200 where you can specify keys and values with various options. 54 00:03:01,200 --> 00:03:05,050 One of those is the to key, which specifies which controller and 55 00:03:05,050 --> 00:03:07,870 which action method that request should be sent to. 56 00:03:07,870 --> 00:03:11,250 So we type a key of to, and the value should be a string. 57 00:03:11,250 --> 00:03:13,830 The first part of the string is the name of the controller we want, 58 00:03:13,830 --> 00:03:16,850 with the word controller left off for brevity. 59 00:03:16,850 --> 00:03:21,380 We want our requests to go to the PagesController, so we'll type pages here. 60 00:03:21,380 --> 00:03:23,960 The second part of the string specifies which of the controller's 61 00:03:23,960 --> 00:03:26,630 action methods should handle the request. 62 00:03:26,630 --> 00:03:30,640 In Ruby documentation, instance methods of a class are marked with a hash mark, 63 00:03:30,640 --> 00:03:33,330 and the same convention is followed here. 64 00:03:33,330 --> 00:03:34,770 So type a hash mark, and 65 00:03:34,770 --> 00:03:39,250 then the name of the action method we want to be called, which is index. 66 00:03:39,250 --> 00:03:42,440 In plain English you could read this as, get requests for 67 00:03:42,440 --> 00:03:48,210 the /pages path should go to the PagesController's index method. 68 00:03:48,210 --> 00:03:54,050 If you go back to the Terminal now and run bin/rails routes again, 69 00:03:54,050 --> 00:03:56,560 you'll see a new route on the end of the list. 70 00:03:56,560 --> 00:03:58,285 It directs get requests for 71 00:03:58,285 --> 00:04:02,040 the /pages path to the PagesController's index method. 72 00:04:03,220 --> 00:04:06,830 Now that we have a route, let's try visiting the /pages path again, and 73 00:04:06,830 --> 00:04:08,700 see if the routing error goes away. 74 00:04:08,700 --> 00:04:11,590 In case you're tired of typing Rails server all the time, 75 00:04:11,590 --> 00:04:14,100 let me give you a preview of a handy shortcut. 76 00:04:14,100 --> 00:04:17,068 You can just type the letter s in place of server, so 77 00:04:17,068 --> 00:04:20,640 we'll run bin/rails s to launch our server. 78 00:04:22,070 --> 00:04:26,525 Now we'll reload our page, and the original error is gone. 79 00:04:26,525 --> 00:04:29,430 Rails sees the request for these /pages path and 80 00:04:29,430 --> 00:04:31,940 tries to route it to the PagesController. 81 00:04:31,940 --> 00:04:33,810 But that controller doesn't exist yet. 82 00:04:33,810 --> 00:04:37,976 So instead we see the error uninitialized constant PagesController. 83 00:04:37,976 --> 00:04:40,167 We'll fix that problem next. 84 00:04:40,167 --> 00:04:43,088 By the way, if you wanna read more about get routes in Rails, 85 00:04:43,088 --> 00:04:44,990 be sure to check the teacher's notes.