在Rails 5.2中我有一个这样的路由文件:
Rails.application.routes.draw do
resources :posts do
resource :comments
end
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
为什么没有/posts/:post_id/comments
路线列出该帖子的所有评论(comments#index
)?和/posts/:post_id/comments/:id
路线以显示单个特定评论。路线如下所示:
>rake routes
Prefix Verb URI Pattern Controller#Action
new_post_comments GET /posts/:post_id/comments/new(.:format) comments#new
edit_post_comments GET /posts/:post_id/comments/edit(.:format) comments#edit
post_comments GET /posts/:post_id/comments(.:format) comments#show
PATCH /posts/:post_id/comments(.:format) comments#update
PUT /posts/:post_id/comments(.:format) comments#update
DELETE /posts/:post_id/comments(.:format) comments#destroy
POST /posts/:post_id/comments(.:format) comments#create
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
这是自Rails 4.2以来的更改吗?这是我第一次玩5.2,而我的大部分经验是在3.2之前。
答案 0 :(得分:3)
为什么没有/ posts /:post_id / comments路线
有:
post_comments GET /posts/:post_id/comments(.:format) comments#show
[为什么不在那里] / posts /:post_id / comments /:id路径以显示单个特定评论
因为您为resource
使用了comments
(单数),所以表示没有多个注释。似乎您想使用resources :comments
。