嵌套资源的 Rails 路由索引

brc*_*ebn 2 indexing routes ruby-on-rails

我正在寻找rake routesindex嵌套资源路径不匹配的原因。

这是我的代码:

namespace :api do
  resources :photos do
    resource :comments
  end
end
Run Code Online (Sandbox Code Playgroud)

这是命令的结果: 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
Run Code Online (Sandbox Code Playgroud)

我试图添加only: [:create, :index]到我的comments资源中,但只有create路线可见。

根据有关嵌套资源的文档,我不明白发生了什么。

感谢您的帮助。

Ric*_*eck 5

这是因为您使用的是单一资源( resource :comments)

从文档:

有时,您有一个资源,客户端总是在不引用 ID 的情况下查找该资源。例如,您希望 /profile 始终显示当前登录用户的个人资料。在这种情况下,您可以使用单一资源将 /profile(而不是/profile/:id)映射到 show 操作

您需要使用标准resources方法来使其正常工作(resource省略index操作):

#config/routes.rb
namespace :api do
  resources :photos do
    resources :comments
  end
end
Run Code Online (Sandbox Code Playgroud)