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.
You need to sign up for Treehouse in order to download course files.Sign up