Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Creating a View1:29 with Jay McGavren
We've set up a route so that the user's browser request makes it to the index action method on the PagesController. But the controller is attempting to render a view, and we haven't created one. So the request is failing. Let's create one now.
When looking for an ERB template so it can render an HTML response, Rails will look for a template file in this location:
<controller> is the name of the current controller (with the word
Controller left off), and
<action> is the name of the current action method.
You can read more in the official Rails documentation.
We're getting a new error when we try to load the /pages path. 0:00 PagesController#index is missing a template. 0:03 We've set up a route so that the user's browser request makes it to the index 0:07 action method on the pages controller. 0:11 But the controller is attempting to render it mute and we haven't created one. 0:13 So the request is failing. 0:17 Our request is being processed by the pages controller. 0:19 By default, rails is going to look in the amp views directory 0:22 in a sub-directory with the same name as the controller. 0:26 The pages sub-directory was created automatically for 0:28 us back when we generated the pages controller. 0:31 The index action method is handling our request so rails will look for 0:33 any ERB template file with the same name as the action by default. 0:38 Here in the pages sub-directory let's create a file named 0:42 index.html.erb that's in all lowercase. 0:45 Rails is going to look for this specific file name. 0:49 So make sure you don't make any typos in the filename. 0:52 We won't do anything fancy right now. 0:55 Let's just get the template displaying something. 0:57 We'll add a Level One heading, Blog Pages. 1:00 Then will save the file. 1:07 Now if we go back to the browser and 1:09 refresh the page, the template will be rendered and we'll see our heading. 1:11 Now we're routing the browser request controller, rendering a view, and 1:16 sending a response back to the browser without any errors. 1:20 We're not really showing anything useful though. 1:23 Up next we're going to create a model class to hold page data. 1:26
You need to sign up for Treehouse in order to download course files.Sign up