我在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
答案 0 :(得分:2)
:only
选项告诉Rails只创建指定的路由。现在,如果将空数组传递给唯一选项,它将不会创建任何选项,因为它期望数组中的操作名称。
所以这会起作用
resources :dishes, only: [] do
参考:http://guides.rubyonrails.org/v3.2.9/routing.html(4.6限制创建的路线)
答案 1 :(得分:0)
你可以做类似的事情
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