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 trial

Ruby Rails Routes and Resources A Route to a Read Action Set Up a URL Parameter

Caitlin Palmer-Bright
Caitlin Palmer-Bright
11,310 Points

Rails 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?

routes.rb
Rails.application.routes.draw do
  # YOUR CODE HERE
  get '/pets', to: 'pets#index'
  get '/pets/:id', to: 'pages/#show' 
end

2 Answers

Hi, 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"

As 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
Caitlin Palmer-Bright
11,310 Points

Oops! Pages instead of pets. I've fixed that but am still getting the same error?

Rails.application.routes.draw do
  # YOUR CODE HERE
  get '/pets', to: 'pets#index'
  get '/pets/:id', to: 'pets#show' 
end

Caitlin, 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
Caitlin Palmer-Bright
11,310 Points

Thanks! Taking out the first 'get' statement did it!

I'm confused, why wouldn't you have to tell the application to route /pets to index?