Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Creating a Show View2:01 with Jay McGavren
We're displaying some raw text for individual Page records, but we'd like to display a full HTML page. For that we're going to need an ERB template.
This video shows a call to
render text:, which works fine in Rails 5.0 (the recommended version to use when following along with this course). But if you happen to have generated your app using Rails 5.1 or later,
render text: no longer works.
In both Rails 5.0 and 5.1, you can replace
render text: with
render plain:, and it will work correctly.
We're displaying some raw text for individual page records, but
we'd like to display a full HTML page.
For that we're going to need in ERB template.
First, we need to get rid of the rendered text line from our controller.
Rails only renders one thing per controller action, so if we left
the render text call in it would prevent our template from being rendered.
Now let's create a template file.
Our request is being processed by the pages controllers show method.
So by default rails will look for a template in the app,
views, pages directory in a file named show.html.erb.
Again watch out for typos in the filename.
We don't have to loop over a collection of pages this time.
We have only a single page stored in the page instance variable.
So putting together an ERB template should be easy.
First let's create a level one heading for the movie title so
that it's displayed in large text.
We'll set up a static h1 element in the middle of that we use an output
And we'll display the title attribute of the object in the page instance variable.
We'll use similar code to show the page body, but
we use a paragraph element instead of a heading.
Within that we'll embed the body attribute.
So when at embedding tag page.body, and close the embedding tag.
Save that, and go back to our list of all pages.
Now if we click the link for an individual page the HTML view will be rendered.
We'll see a heading with the page title and a paragraph with the page body.
That's another tricky operation complete.
You've learned how to create links that send a request for
a particular model object.
More importantly, you've learned how to take those requests,
find the requested object, and display it in a view.
You're doing great.
Don't stop now though, because next we're going to learn how to
handle requests to create new model objects.
You need to sign up for Treehouse in order to download course files.Sign up