仅设置成员路由时排除CRUD资源
Excluding CRUD resources when only setting up member routes
我已将 Dish
操作的路线嵌套在 Restaurants
的路线中。唯一的问题是我只需要基于成员的路由而不是标准的基于 CRUD 的操作。有没有一种方法可以将它们全部排除,而不必像这样完全写出每个资源:
resources :dishes, except: [:new, :create, :edit, :update, :show, :destroy] do
...
end
或
resources :dishes, only: [:like, :unlike, :dislike, :undislike] do
...
end
当前设置
resources :restaurants do
resources :dishes do
member do
put "like", to: "dishes#like"
put "unlike", to: "dishes#unlike"
put "dislike", to: "dishes#dislike"
put "undislike", to: "dishes#undislike"
end
end
end
你可以这样做
resources :restaurants do
resources :dishes, only: [] do
member do
put "like", to: "dishes#like"
put "unlike", to: "dishes#unlike"
put "dislike", to: "dishes#dislike"
put "undislike", to: "dishes#undislike"
end
end
end
:only
选项告诉 Rails 只创建指定的路由。现在,如果您将空数组传递给唯一选项,它将不会创建任何内容,因为它需要数组中的操作名称。
所以这会起作用
resources :dishes, only: [] do
参考:http://guides.rubyonrails.org/v3.2.9/routing.html(4.6限制创建的路由)
我已将 Dish
操作的路线嵌套在 Restaurants
的路线中。唯一的问题是我只需要基于成员的路由而不是标准的基于 CRUD 的操作。有没有一种方法可以将它们全部排除,而不必像这样完全写出每个资源:
resources :dishes, except: [:new, :create, :edit, :update, :show, :destroy] do
...
end
或
resources :dishes, only: [:like, :unlike, :dislike, :undislike] do
...
end
当前设置
resources :restaurants do
resources :dishes do
member do
put "like", to: "dishes#like"
put "unlike", to: "dishes#unlike"
put "dislike", to: "dishes#dislike"
put "undislike", to: "dishes#undislike"
end
end
end
你可以这样做
resources :restaurants do
resources :dishes, only: [] do
member do
put "like", to: "dishes#like"
put "unlike", to: "dishes#unlike"
put "dislike", to: "dishes#dislike"
put "undislike", to: "dishes#undislike"
end
end
end
:only
选项告诉 Rails 只创建指定的路由。现在,如果您将空数组传递给唯一选项,它将不会创建任何内容,因为它需要数组中的操作名称。
所以这会起作用
resources :dishes, only: [] do
参考:http://guides.rubyonrails.org/v3.2.9/routing.html(4.6限制创建的路由)