Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
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.
The 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