Rails嵌套路由与控制器

时间:2015-07-15 21:55:25

标签: ruby-on-rails ruby ruby-on-rails-4 routes

我有这些路线:

      namespace :api, defaults: {format: :json} do


      namespace :v1 do
      resources :cars

      resources :users, only: [:create] do
        member do
          resources :cars, controller: "user/cars"
          resources :friends, controller: "user/friends"
          resources :drink, controller: "user/drinks"
        end
      end

    end
  end

有没有办法在那些嵌套资源上抽象非干控制器定义?

编辑:

我理想的示例输出是:

namespace:api,默认值:{format :: json} do     namespace:v1 do

  resources :cars

  resources :users, only: [:create] do
    member do
      resources :cars
      resources :friends
      resources :drink
    end
  end

end

并且三个嵌套资源使用之前声明的控制器

1 个答案:

答案 0 :(得分:0)

经过研究,我最终提出了这个问题:

  namespace :api, defaults: {format: :json} do
    namespace :v1 do
      resources :users, only: [:create]
      resources :cars
      scope "users/:id", as: "users", module: "users" do
         resources :cars
         resources :friends
         resources :drink
      end
    end
  end