Finding a Page2:55 with Jay McGavren
We're trying to show individual Page records, and we've set up a route to the "show" method on the "PagesController". But that method doesn't exist yet. Let's visit the PagesController code, and add a show method now.
This video recommends calling
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 trying to show individual page records, and 0:00 we've set up a route to the show method on the pages controller. 0:02 But that method doesn't exist yet. 0:06 Let's visit the pages controller code and add a show method now. 0:08 Within our index method here, we load a list of all page objects and 0:15 store them in an instance variable. 0:19 In the show method, we need to load one specific page object. 0:21 But how do we know which one? 0:25 In the routes.rb file, we set our route up with a URL parameter named, id. 0:26 Rails takes the portion of the path that's at the same place as this parameter and 0:33 makes it available within the controller as params [:id]. 0:37 Since our links incorporate the ID of each page object. 0:43 We just have to access params [:id] in to get the ID of the page to load. 0:46 Let's use a trick to look at the parameter directly. 0:50 Instead of rendering a template, which Rails does by default. 0:53 We're going to render a simple text string with the ID. 0:56 In our show method, we'll call render. 0:59 [SOUND] With an argument keyword of text. 1:01 And a value of params [:id]. 1:08 Now, lets reload this /pages/4 path in our browser. 1:11 The pages controller show method will be called and display the ID parameter, 4. 1:15 Now let's try typing into the URL bar, /pages/5. 1:21 We'll see an ID of 5. 1:25 Let's try updating it to /pages/gibberish. 1:27 And we'll see an ID of gibberish. 1:32 Although obviously, this wouldn't be a valid database ID. 1:34 The same is true for links we click from the index of all pages. 1:38 The part of the link path following /pages/ is treated as an ID parameter. 1:41 Now let's use the ID back in the controller to look up 1:47 a particular page record. 1:50 In the index method, 1:52 we assign the collection of all pages to an instance variable named, pages floral. 1:53 But since we're working with just one page here, 1:58 we'll name the instance variable page, singular. 2:00 [SOUND] We'll call page.find. 2:04 Pass params [:id] as an argument. 2:12 And the page object that gets returned will be assigned to the instance variable. 2:18 We'd like to test this out, but we still don't have a view set up. 2:22 So instead, will use rendered text again to display the page title. 2:25 Save our work and go back to our browser to the list of all pages. 2:33 And now, when we click on the link for any page. 2:38 The ID from the path gets used to look up that page record and 2:42 the page's title gets displayed. 2:45 So we have proof that our record's being accessed. 2:48 But we can't really display useful information without a view. 2:50 We'll create a view, next. 2:53
You need to sign up for Treehouse in order to download course files.Sign up