grouping controller in subdirectories for nested resources

时间秒杀一切 提交于 2019-11-30 05:02:46
nowk

If you want to use just that one route:

match 'locations/:location_id/users' => "locations/users#index"

That should come before any other resources/matches that might conflict with that match. By default Rails routes are top-bottom.

# should be before locations resource
resources :locations do
  resources :users
end

Alternatively, if you want to punt all your nested users resource over to locations/users you can assign a controller to the resource.

resources :locations do
  resources :users, :controller => "locations/users"
end

One can use modules to have nested routes with nested controllers:

resources :locations do
  scope module: :locations do
    resources :users
  end
end

$ rake routes

...
location_users GET /locations/:location_id/users locations/users#index
...

Like Kwon says, it's the order that matters. But you can still use a namespace.

.../config/routes.rb

namespace :locations do
    resources :users
end
resources :locations

.../app/controllers/locations_controller.rb:

class LocationController < ApplicationController

.../app/controllers/locations/users_controller.rb:

class Locations::UsersController < LocationsController
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!