brcebn
brcebn

Reputation: 1732

Rails routes index for nested resource

I'm searching a reason why rake routes doesn't match the index path of my nested resource.

Here is my code:

namespace :api do
  resources :photos do
    resource :comments
  end
end

Here is the result of the command: rake routes | grep comment

           batch_action_admin_user_comments POST       /admin/user_comments/batch_action(.:format)            admin/user_comments#batch_action
                        admin_user_comments GET        /admin/user_comments(.:format)                         admin/user_comments#index
                                            POST       /admin/user_comments(.:format)                         admin/user_comments#create
                     new_admin_user_comment GET        /admin/user_comments/new(.:format)                     admin/user_comments#new
                    edit_admin_user_comment GET        /admin/user_comments/:id/edit(.:format)                admin/user_comments#edit
                         admin_user_comment GET        /admin/user_comments/:id(.:format)                     admin/user_comments#show
                                            PATCH      /admin/user_comments/:id(.:format)                     admin/user_comments#update
                                            PUT        /admin/user_comments/:id(.:format)                     admin/user_comments#update
                                            DELETE     /admin/user_comments/:id(.:format)                     admin/user_comments#destroy
                             admin_comments GET        /admin/comments(.:format)                              admin/comments#index
                                            POST       /admin/comments(.:format)                              admin/comments#create
                              admin_comment GET        /admin/comments/:id(.:format)                          admin/comments#show
                         api_photo_comments POST       /api/photos/:photo_id/comments(.:format)               api/comments#create
                     new_api_photo_comments GET        /api/photos/:photo_id/comments/new(.:format)           api/comments#new
                    edit_api_photo_comments GET        /api/photos/:photo_id/comments/edit(.:format)          api/comments#edit
                                            GET        /api/photos/:photo_id/comments(.:format)               api/comments#show
                                            PATCH      /api/photos/:photo_id/comments(.:format)               api/comments#update
                                            PUT        /api/photos/:photo_id/comments(.:format)               api/comments#update
                                            DELETE     /api/photos/:photo_id/comments(.:format)               api/comments#destroy

I tried to add only: [:create, :index] to my comments resource but only the create route is visible.

According to the documentation about nested-resources I don't understand what's happening.

Thank you for your help.

Upvotes: 2

Views: 940

Answers (2)

Richard Peck
Richard Peck

Reputation: 76774

It's because you are using a singular resource (resource :comments)

From the docs:

Sometimes, you have a resource that clients always look up without referencing an ID. For example, you would like /profile to always show the profile of the currently logged in user. In this case, you can use a singular resource to map /profile (rather than /profile/:id) to the show action

You'll need to use the standard resources method to get this working (resource omits the index action):

#config/routes.rb
namespace :api do
  resources :photos do
    resources :comments
  end
end

Upvotes: 6

brcebn
brcebn

Reputation: 1732

My mistake. A "S" was missing on my resource.

namespace :api do
  resources :photos do
    resources :comments
  end
end

Now it works.

Upvotes: 0

Related Questions