【问题标题】:Excluding CRUD resources when only setting up member routes仅设置成员路由时排除 CRUD 资源
【发布时间】:2015-10-24 19:06:48
【问题描述】:

我已经为 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 :dishes, only: [] do 。这不会生成 CRUD 路由。

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


【解决方案1】:

:only 选项告诉 Rails 只创建指定的路由。现在,如果您将空数组传递给唯一选项,它将不会创建任何数组,因为它需要数组中的动作名称。

这样就可以了

resources :dishes, only: [] do

参考:http://guides.rubyonrails.org/v3.2.9/routing.html(4.6 限制创建的路由)

【讨论】:

    【解决方案2】:

    你可以这样做
    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-09
      相关资源
      最近更新 更多