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 trialCaitlin Palmer-Bright
11,310 PointsRails Routes and Resources Challenge url parameters
On the 'Set Up a URL Parameter' challenge, and I have no idea what I'm doing. Thought I'd been following along well with the videos up til this point but this has me stumped!
Instructions are: Set up a route to view an individual Pet. It should match GET requests with a path of /pets/ followed by the ID of the particular Pet: /pets/3, /pets/27, etc. You'll need to set it up so that the ID from the path is available within the controller as params[:id]. Matching requests should be routed to the show action method on PetsController.
The error i'm getting is: Your route's path needs to include an "id" URL parameter.
Any suggestions?
Rails.application.routes.draw do
# YOUR CODE HERE
get '/pets', to: 'pets#index'
get '/pets/:id', to: 'pages/#show'
end
2 Answers
Ethan M
10,637 PointsHi, you are so close and it's normal for these little mistakes but if you do not understand rails routing I strongly recommend reading this Understanding Rails Routing Rails 5 surely updated with the routes from when I used it haha
Anyway, you are probably interested in the answer Original:
Rails.application.routes.draw do
# YOUR CODE HERE
get '/pets', to: 'pets#index'
get '/pets/:id', to: 'pages/#show'
end
Corrected:
Rails.application.routes.draw do
get '/pets/:id', to: 'pets#show'
end
It looking for the pets directory and the show.html.erb file. Essentially saying "Hey GET me pet #3"
Derek Lin
1,563 PointsI'm confused, why wouldn't you have to tell the application to route /pets to index?
Ethan M
10,637 PointsEthan M
10,637 PointsAs you can see from the original code you wrote you pretty much nailed it but that little mistake caused that error haha
Caitlin Palmer-Bright
11,310 PointsCaitlin Palmer-Bright
11,310 PointsOops! Pages instead of pets. I've fixed that but am still getting the same error?
Ethan M
10,637 PointsEthan M
10,637 PointsCaitlin, you do not need the get '/pets', to: "pets#index" it is not required for receiving a specific route since we tell our router where to go and what to get. It already knows it's in the /pets directory and it'll then look for the id passed in the url then the show will process it based on the given id :)
Caitlin Palmer-Bright
11,310 PointsCaitlin Palmer-Bright
11,310 PointsThanks! Taking out the first 'get' statement did it!