仅在设置成员路由时排除CRUD资源

时间:2015-08-02 17:04:48

标签: ruby-on-rails rest model-view-controller routes resources

我在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

2 个答案:

答案 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