轨道嵌套路由未按预期工作



我在我的路由中嵌套了这样的资源。这些在我的其他 rails 5 应用程序上完美运行,但在我的 rails 6 应用程序上则不然。我不明白为什么它只识别第一级嵌套的东西。

resources :blogs do
member do
put 'like', to: 'blogs#upvote'
put 'dislike', to: 'blogs#downvote'
end
resources :comments
member do
put 'like', to: 'comments#upvote'
put 'dislike', to: 'comments#downvote'
end
resources :notations
end

这是耙子路线给我的:

blogs_user GET      /users/:id/blogs(.:format)                                                               users#blogs
like_blog PUT      /blogs/:id/like(.:format)                                                                blogs#upvote
dislike_blog PUT      /blogs/:id/dislike(.:format)                                                             blogs#downvote
blog_comments GET      /blogs/:blog_id/comments(.:format)                                                       comments#index
POST     /blogs/:blog_id/comments(.:format)                                                       comments#create
new_blog_comment GET      /blogs/:blog_id/comments/new(.:format)                                                   comments#new
edit_blog_comment GET      /blogs/:blog_id/comments/:id/edit(.:format)                                              comments#edit
blog_comment GET      /blogs/:blog_id/comments/:id(.:format)                                                   comments#show
PATCH    /blogs/:blog_id/comments/:id(.:format)                                                   comments#update
PUT      /blogs/:blog_id/comments/:id(.:format)                                                   comments#update
DELETE   /blogs/:blog_id/comments/:id(.:format)                                                   comments#destroy
PUT      /blogs/:id/like(.:format)                                                                comments#upvote
PUT      /blogs/:id/dislike(.:format)                                                             comments#downvote
notations GET      /blogs/:id/notations(.:format)                                                           notations#index
POST     /blogs/:id/notations(.:format)                                                           notations#create
new_notation GET      /blogs/:id/notations/new(.:format)                                                       notations#new
edit_notation GET      /blogs/:id/notations/:id/edit(.:format)                                                  notations#edit
notation GET      /blogs/:id/notations/:id(.:format)                                                       notations#show
PATCH    /blogs/:id/notations/:id(.:format)                                                       notations#update
PUT      /blogs/:id/notations/:id(.:format)                                                       notations#update
DELETE   /blogs/:id/notations/:id(.:format)                                                       notations#destroy

例如,在我的另一个应用程序上,它会产生

/blogs/:blog_id/comments/:id/like

我复制了您的路线并在两个应用程序(Rails 5 和 Rails 6(中复制,并且都生成了相同的路线(没有三个嵌套级别(。如果你想要/blogs/:blog_id/comments/:id/like路线,你必须做一个小的改变。

resources :blogs do
member do
put 'like', to: 'blogs#upvote'
put 'dislike', to: 'blogs#downvote'
end
resources :comments do
member do
put 'like', to: 'comments#upvote'
put 'dislike', to: 'comments#downvote'
end
end
resources :notations
end

你缺少"do"end"块语法

resources :blogs do
member do
put 'like', to: 'blogs#upvote'
put 'dislike', to: 'blogs#downvote'
end
resources :comments do # here
member do
put 'like', to: 'comments#upvote'
put 'dislike', to: 'comments#downvote'
end
resources :notations
end # and here
end

无论如何,轨道指南不鼓励超过两个级别的嵌套。

最新更新