Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialYiheng Chu
8,465 PointsWhy I can't use redirect_to '/pages/:id'? It said the 'id' params doesn't passed into the #show action.
All info is above. Thank you!
1 Answer
Jay McGavren
Treehouse Teacherredirect_to("/pages/:id")
will literally redirect the browser to load the path "/pages/:id"
. So if you have a get "/pages/:id", to: "pages#show"
route set up, then when show
is called, params[:id]
will be set to the string ":id"
, which is not what you want.
Instead, you should pass redirect_to
a model object that has its id
attribute set:
redirect_to @page
Or you should interpolate the ID into a string, and pass that to redirect_to
:
redirect_to "/pages/#{@page.id}"
Here's what it would look like in a controller action:
def create
@page = Page.new(page_params)
if @page.save
redirect_to @page, notice: 'Page was successfully created.'
else
render :new
end
end