gpt4 book ai didi

ruby-on-rails - 嵌套资源的 Rails 路线索引

转载 作者:行者123 更新时间:2023-12-01 22:46:58 25 4
gpt4 key购买 nike

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

这是我的代码:

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

这是命令的结果:rake paths | grep 注释

           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

我尝试将 only: [:create, :index] 添加到我的 comments 资源,但只有 create 路由可见。

根据有关nested-resources的文档我不明白发生了什么事。

感谢您的帮助。

最佳答案

这是因为您使用的是singular resource (资源:评论)

来自文档:

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

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

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

关于ruby-on-rails - 嵌套资源的 Rails 路线索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23696447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com