我在路线中这样嵌套资源。这些可以在我的其他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
答案 0 :(得分:1)
您缺少“ 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
无论如何,Rails指南不建议嵌套两个以上级别。
答案 1 :(得分:1)
我复制了您的路线,并复制到两个应用程序(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