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.
PagesController#index is missing a template.
We've set up a route so that the user's browser request makes it to the index
action method on the pages controller.
But the controller is attempting to render it mute and we haven't created one.
So the request is failing.
Our request is being processed by the pages controller.
By default, rails is going to look in the amp views directory
in a sub-directory with the same name as the controller.
The pages sub-directory was created automatically for
us back when we generated the pages controller.
The index action method is handling our request so rails will look for
any ERB template file with the same name as the action by default.
Here in the pages sub-directory let's create a file named
index.html.erb that's in all lowercase.
Rails is going to look for this specific file name.
So make sure you don't make any typos in the filename.
We won't do anything fancy right now.
Let's just get the template displaying something.
We'll add a Level One heading, Blog Pages.
Then will save the file.
Now if we go back to the browser and
refresh the page, the template will be rendered and we'll see our heading.
Now we're routing the browser request controller, rendering a view, and
sending a response back to the browser without any errors.
We're not really showing anything useful though.
Up next we're going to create a model class to hold page data.
You need to sign up for Treehouse in order to download course files.Sign up