Creating a Controller2:39 with Jay McGavren
We have a route set up that says GET requests for /pages should go to the PagesController index method. But that controller doesn't exist yet, so the request is failing. Let's create a controller to handle requests for Page resources.
We have a route set up that says GET requests for /pages should go to the pages 0:00 controller index method but that controller doesn't exist yet. 0:04 So the request is failing. 0:08 Let's create a controller to handle requests for page resources. 0:10 We can do that using a Rails generator. 0:13 Just as there is a scaffold generator and 0:15 a migration generator, there's also a controller generator. 0:18 So in our terminal, we need to cancel out of the Rails Server if it's running, 0:21 and then we type bin/rails generate controller. 0:27 And then the name of the controller we want to generate. 0:31 The name of the controller should be the plural form of your resource. 0:34 So since this is for page resources, the plural is pages. 0:37 Whatever you do, don't type controller on the end of the controller name. 0:42 If you do, you'll wind up with a file name pages_controller_controller. 0:46 And you don't want that. 0:50 If you ever make a mistake like that, here's a handy way to undo it. 0:52 Run the same command but replace generate with destroy. 0:55 That will automatically delete all the misnamed files. 1:03 Obviously the destroy subcommand is a little dangerous. 1:06 So don't use it if you've manually added code to any of those files. 1:09 Now let's rerun our generate command with the correct resource name, pages. 1:13 That will automatically generate a pages_controller.rb file in your 1:19 app/controllers subdirectory, along with our app's other controllers. 1:23 The word controller will automatically be added onto the name for you. 1:27 If we look at that file in our editor we see that it just defines an empty class 1:32 named PagesController that inherits from another class named ApplicationController. 1:36 There aren't any action methods defined yet, but 1:41 now that the controller exists, let's try our request again and see what happens. 1:43 We'll launch our server with bin/rails s. 1:48 Then we'll go back to the browser and reload the page. 1:53 The uninitialized constant PagesController error goes away. 1:56 Instead we see an unknown action error, the action index could not be found for 2:00 PagesController. 2:05 Let's add that index action method now. 2:06 In your editor, add def index, end and then save your changes. 2:09 There's no need to restart Rails, it will load our changes automatically. 2:15 Now let's go back to our browser and hit refresh and the route is complete. 2:19 Rails finds the index method it was trying to route to, so 2:24 the unknown action error goes away. 2:26 Now there's yet another error, though. 2:29 PagesController#index is missing a template. 2:31 Our request is being routed to a controller action, but 2:33 we're missing a view. 2:36 We'll fix that next. 2:37
You need to sign up for Treehouse in order to download course files.Sign up