Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Build a Rails API Coding the API Including Nested Resources in JSON Output

Chris Vukin
Chris Vukin
17,787 Points

namespace review v2 nesting

I'm having trouble with the v2 nesting in this last challenge, I keep getting an error saying I need to add todo_lists resource when I've already got that resource listed.. any suggestions are appreciated.

Here's my code:

Rails.application.routes.draw do
  namespace :api do
    namespace :v1 do
      resources :todo_lists do
        resources :todo_items do
          namespace :v2 do
            resources :todo_lists do
              resources :todo_items do
              end
            end
          end
           member do
            patch :complete
          end
          end
        end
      end
  end
end

4 Answers

Hi Chris,

V 1 and V2 are separately way after each end tags, see the example below.

Rails.application.routes.draw do
  namespace :api do
    namespace :v1 do  
      resources :todo_lists do
        resources :todo_items do
          member do
            patch :complete
          end
        end
      end
     end 
   namespace :v2 do  
      resources :todo_lists do
        resources :todo_items do
          member do
            patch :complete
          end
        end
      end
     end     
  end
end

Hope that will work for you. :-)

Sure, the function of member is itself member route to require ID that would acts on a member by using URL and HTTP method, patch is to update the route on :id ( v1 & v2). Yes it would go through both v1 and v2 under same application routes, not separate way.

Chris Vukin
Chris Vukin
17,787 Points

Ah, interesting.. so we needed to include the member do and the patch as well? Could you explain what the function of member is? v1 and v2 are both nested in the namespace :api allowing access to both v1 and v2 thru the api though?

Thanks for your reply!

Konrad Pilch
Konrad Pilch
2,435 Points

Just abit confused as l looked at this. Whats v1 and v2 in the first place you did not make mention of them in her video.