Linking to Pages3:56 with Jay McGavren
Users can view a list of all our page titles, but they can't view the content of single Pages yet. For our Posts resource, they can click a link on the list of all Posts to view an individual post. Let's set up links to view single Pages as well.
link_to method returns a string with the HTML for a link.
<%= link_to("link text", "/link/path") %>
More documentation is here.
Let's say we need to set up links to view individual
Page objects. In the URL path, following
/pages/, we need a string representing the ID of the Page object we want to load. We'll set up the route like this:
get '/pages/:id', to: 'pages#show'
The colon followed by a name is a URL parameter. A URL parameter captures part of the request path, and makes it available within the controller in the
params hash. So if we type :id here, it will be available later as
params[:id]. We can name the parameter whatever we want, but by convention the name :id is used for model record IDs.
[MUSIC] 0:00 Users can view a list of all our page titles but 0:05 they can't view the content of single pages yet. 0:07 For a post resource they can click a link on the list of all posts to view 0:10 an individual post. 0:13 Let's set up links to view single pages as well. 0:15 We wanna turn these page titles into links, so we can click on them and 0:19 view the associated page. 0:23 Remember how we showed you that all saved rails model objects have an ID 0:25 attribute that's used to look them up in the database. 0:28 Creating links is one major reason that ID attribute exists. 0:32 We're going to use that ID in our links to tell rails which model objects to load. 0:36 We can embed dynamic HTML links into the output with output embedding tags. 0:40 Within rails views, a helper method named Link to is available. 0:49 It returns the necessary HTML string for creating a link. 0:53 Link to his first argument should be the link text that you wanna displayed. 0:57 And the second argument should be the path that you want the browser to request. 1:01 If we save this we'll have web links in our output, let's say link text and 1:09 that point to the nonexistent path of slash link, slash path. 1:14 We can see the HTML output in our browser's developer tools. 1:19 There's the link text within the anchor tag and 1:24 there's the path of the link references. 1:26 Now let's update this call to link two and make it do something useful. 1:30 We'll change the link text argument to be the page object's title. 1:34 And we'll change the link path to the double quoted string slash pages slash 1:39 page ID. 1:44 And there's that idea attribute. 1:47 We interpreted into the path string, so 1:49 that the post with an idea of one creates a link to slash pages slash one. 1:51 A post with an idea of three creates a link to slash pages slash three and so on. 1:55 If we save this and reload our browser will see our updated links. 2:01 But we can't actually click on them yet If we're trying will see an error, no route 2:05 matches [GET] /pages/4, or whatever the idea of the link we tried to click on is. 2:09 We need to go into the configs/route.rb file to fix this. 2:16 We've set up a route for slash pages by itself but 2:22 there's no route for slash pages followed by an ID. 2:25 Let's set one up. 2:29 Since the browser is requesting to load a page. 2:30 It's going to be an HTTP get request. 2:32 So, we'll set up another get route. 2:35 We want it to match requests for paths that start with slash pages slash, so 2:37 we'll add that string next. 2:42 But following slash pages slash we're expecting a string representing the ID 2:45 of the page object we want to load, so will type colon ID. 2:49 The colon followed by a name is a URL parameter. 2:54 A URL parameter captures part of the request path and 2:58 makes it available within the controller in the params object. 3:01 You may remember that we used params before to access form submission contents. 3:04 So, if we type colon ID here, 3:09 it will be available later in the params object under the ID key. 3:11 We can name the parameter whatever we want. 3:15 But by convention, the name ID is used for model record IDs. 3:17 On the route for the list of all pages we specify which control or and 3:22 which action should handle the requests. 3:25 We need to do the same for the route for individual pages will add a second 3:27 argument to get with a key of two and a value of pages pound sign show that 3:33 will direct requests of the show action on the pages controller. 3:38 Let's save our work and now if we click on these pages for link of our browser. 3:43 We'll be routed to the show action on page's controller, 3:49 of course we haven't created that method yet but will take care of that in a bit. 3:52
You need to sign up for Treehouse in order to download course files.Sign up