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 Active Record Associations in Rails Using Associations in Your App Nested Routes and Resources

88,487 Points

Help with Nested Routes Challenge. What am I doing wrong?

Challenge Task 1 of 1 Every Pet belongs to an Owner, so we want an owner_id parameter as part of the URL for all of our Pet routes:

Verb URI Pattern Controller#Action GET /owners/:owner_id/pets(.:format) pets#index POST /owners/:owner_id/pets(.:format) pets#create GET /owners/:owner_id/pets/new(.:format) pets#new GET /owners/:owner_id/pets/:id/edit(.:format) pets#edit GET /owners/:owner_id/pets/:id(.:format) pets#show PUT /owners/:owner_id/pets/:id(.:format) pets#update DELETE /owners/:owner_id/pets/:id(.:format) pets#destroy

Make the necessary changes here in config/routes.rb to set all of the above routes up. (You can set up routes for owners as well if you want, but we'll only be checking whether the above routes exist.)

Rails.application.routes.draw do resources :pets do get '/owners/:owner_id/pets(.:format)', to: 'pets#index', as: ‘pets’ post '/owners/:owner_id/pets(.:format)', to: 'pets#create', as: ‘create’ get '/owners/:owner_id/pets/new(.:format)', to: 'pets#new', as: 'new' get '/owners/:owner_id/pets/:id/edit(.:format', to: 'pets#edit', as: ‘edit’ get '/owners/:owner_id/pets/:id(.:format)', to: 'pets#show', as: ‘show’ put '/owners/:owner_id/pets/:id(.:format)', to: 'pets#update' delete '/owners/:owner_id/pets/:id(.:format)', to: 'pets#destroy'

end I'm getting error messages on my answer. Please help. What am I doing wrong?

3 Answers

Dimitri Fajardo
Dimitri Fajardo
Courses Plus Student 6,973 Points
resources :owners do
  resources :pets

I don't know what your doing wrong but I would need to view your code to explain. I hope this helps!

Christopher Phillips
Christopher Phillips
10,061 Points

A misleading challenge. It says setting up Owners is optional yet the requirement to complete the challenge tells you that you must set up owners as shown by the paths that will be checked. I used the same code as Dimitri.

I agree with the two above. Just remember that the objective here is making sure that you are nesting the routes properly. So here, we are to nest under the "owners" resource by putting "do" after "resources :owners." This "do" is needed so that we can add a block. We want to add this block so that we can access the resources we need which, in this case, is the pets resources. So, the block will be "resources :pets." This means that this block is making a call to the pets resources. This call to the pets resources gives us the routes we need without us having to use several, several lines of code.

Also, make sure that you have all of your "end" statements.

Rails.application.routes.draw do resources :owners do resources :pets end end

I am actually a novice coder with so much to learn, so I hope that my explanation was helpful.