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 0:00 we'd like to display a full HTML page. 0:03 For that we're going to need in ERB template. 0:06 First, we need to get rid of the rendered text line from our controller. 0:10 Rails only renders one thing per controller action, so if we left 0:13 the render text call in it would prevent our template from being rendered. 0:17 Now let's create a template file. 0:21 Our request is being processed by the pages controllers show method. 0:22 So by default rails will look for a template in the app, 0:27 views, pages directory in a file named show.html.erb. 0:32 Again watch out for typos in the filename. 0:40 We don't have to loop over a collection of pages this time. 0:44 We have only a single page stored in the page instance variable. 0:47 So putting together an ERB template should be easy. 0:51 First let's create a level one heading for the movie title so 0:54 that it's displayed in large text. 0:57 We'll set up a static h1 element in the middle of that we use an output 0:58 embedding tag. 1:02 And we'll display the title attribute of the object in the page instance variable. 1:05 We'll use similar code to show the page body, but 1:13 we use a paragraph element instead of a heading. 1:15 Within that we'll embed the body attribute. 1:18 So when at embedding tag page.body, and close the embedding tag. 1:21 Save that, and go back to our list of all pages. 1:27 Now if we click the link for an individual page the HTML view will be rendered. 1:32 We'll see a heading with the page title and a paragraph with the page body. 1:37 That's another tricky operation complete. 1:42 You've learned how to create links that send a request for 1:45 a particular model object. 1:47 More importantly, you've learned how to take those requests, 1:48 find the requested object, and display it in a view. 1:51 You're doing great. 1:54 Don't stop now though, because next we're going to learn how to 1:55 handle requests to create new model objects. 1:58
You need to sign up for Treehouse in order to download course files.Sign up