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
Before we fix the controller issue, we need to take a quick detour and show you a better way to create links within your own app. Having to type "/pages/#{page.id}" everywhere you want to link to an individual page is tedious and error-prone. If we want, we can name the route. This will have Rails create a path helper method for us that will return that same path string.
Having to type "/pages/#{page.id}"
everywhere you want to link to an individual page is tedious and error-prone. If we want, we can name the route. This will have Rails create a path helper method for us that will return that same path string.
In routes.rb, add an as: argument at the next of the route, with a string value. That string will be used as the route name. It can be anything we want, but since this is a route to get a single Page, a name of 'page' would be conventional.
get '/pages/:id', to: 'pages#show', as: 'page'
Now, in your templates, you can replace "/pages/#{page.id}"
with page_path(page)
. It will look at the ID of the model object you pass in, and return a string with a path that includes that ID.
You need to sign up for Treehouse in order to download course files.
Sign up