Creating a Route4:45 with Jay McGavren
Rails lets you set up "routes" for requests, so that you can send a particular request to a controller that can handle it.
To set up a route to the index of all
Page objects, we add a line like this in
get '/pages', to: 'pages#index'`
In plain English, you could read this as "GET requests for the '/pages' path should go to the PagesController's 'index' method".
The result is a route like this:
$ bin/rails routes Prefix Verb URI Pattern Controller#Action pages GET /pages(.:format) pages#index
You can read more about GET routes in the official Rails documentation.
A given Rails app is likely to have a whole bunch of resources, 0:00 each with its own controller to handle requests for them. 0:04 In our Blog app requests to create, read, update and 0:07 delete posts are handled by the PostController. 0:10 Now we need to add a PagesController to handle current requests for pages. 0:13 But how will the requests get to the PagesController? 0:17 How do we tell a request for a post apart from a request for a page? 0:20 Rails lets you set up routes for requests. 0:24 So that you can send a particular request to a controller that can handle it. 0:26 We'll look at how to set up routes next. 0:29 We're here in our Blog app viewing the /post path. 0:33 It displays a list of all our post model objects. 0:37 In this stage, we're gonna set up a list of all our page model objects. 0:40 We're going to want that list to be available at the /pages path. 0:44 But let's see what happens if we try to visit that path now. 0:48 We'll change /post here in the URL bar to /pages. 0:50 We see an error. 0:59 No route matches get /pages. 1:00 So what's this route it's referring to? 1:03 Your Rails app has several controllers, each with several actions methods. 1:05 When Rails receives a request from a browser, 1:09 it needs to know which controller action to send it to. 1:12 To determine that, it looks at the http request type and the path. 1:15 Remember, that's the part of the URL following the server and port. 1:20 Then Rails looks for a route to determine which controller and 1:23 which method should handle that request. 1:26 So you might have a route that says, get requests for 1:29 the /pages path should be sent to the PagesController's index method. 1:32 Whenever Rails receives a get request for /pages, 1:37 it'll call the index method on the PagesController. 1:40 You can get a list of all the routes in a Rails app from your Terminal. 1:44 Stop the server if it's running, 1:47 and type bin/rails routes. 1:52 Windows users, don't forget to type ruby bin\rails instead of bin/rails. 1:56 There are lots of routes for post resources but none for page resources. 2:01 We're going to need to have one. 2:05 A Rails application's routes are configured within the config directory. 2:07 In the routes.rb file. 2:11 This file contains Ruby code that sets up the routes. 2:15 The syntax can be a little confusing, 2:18 especially if you're not used to keyword arguments on Ruby methods. 2:20 Don't worry about fully understanding it right now. 2:24 If you copy the examples we show you here you'll be able to modify them 2:26 to suit your needs. 2:30 Let's add a route for the /pages path now. 2:31 Remember, there are several types of HTTP requests, such as get and post. 2:34 When your browser sends a request to view a page, it's a get request. 2:39 So we'll call the get method to set up a get route. 2:43 As an argument to the get method, 2:47 we need to provide the path that this route should match as a string. 2:48 So we'll start a string and type /pages. 2:52 The next argument to the get method is a hash, 2:55 where you can specify keys and values with various options. 2:57 One of those is the to key, which specifies which controller and 3:01 which action method that request should be sent to. 3:05 So we type a key of to, and the value should be a string. 3:07 The first part of the string is the name of the controller we want, 3:11 with the word controller left off for brevity. 3:13 We want our requests to go to the PagesController, so we'll type pages here. 3:16 The second part of the string specifies which of the controller's 3:21 action methods should handle the request. 3:23 In Ruby documentation, instance methods of a class are marked with a hash mark, 3:26 and the same convention is followed here. 3:30 So type a hash mark, and 3:33 then the name of the action method we want to be called, which is index. 3:34 In plain English you could read this as, get requests for 3:39 the /pages path should go to the PagesController's index method. 3:42 If you go back to the Terminal now and run bin/rails routes again, 3:48 you'll see a new route on the end of the list. 3:54 It directs get requests for 3:56 the /pages path to the PagesController's index method. 3:58 Now that we have a route, let's try visiting the /pages path again, and 4:03 see if the routing error goes away. 4:06 In case you're tired of typing Rails server all the time, 4:08 let me give you a preview of a handy shortcut. 4:11 You can just type the letter s in place of server, so 4:14 we'll run bin/rails s to launch our server. 4:17 Now we'll reload our page, and the original error is gone. 4:22 Rails sees the request for these /pages path and 4:26 tries to route it to the PagesController. 4:29 But that controller doesn't exist yet. 4:31 So instead we see the error uninitialized constant PagesController. 4:33 We'll fix that problem next. 4:37 By the way, if you wanna read more about get routes in Rails, 4:40 be sure to check the teacher's notes. 4:43
You need to sign up for Treehouse in order to download course files.Sign up