DEV Community

loading...

Discussion on: Organizing Routes In Rails

Collapse
andrewbrown profile image
Andrew Brown 🇨🇦 • Edited

Here's an alternate way.
No need to use extends or modularize your routes.

# config/application.rb
module MyApp
  class Application < Rails::Application
    config.paths['config/routes.rb'].concat Dir[Rails.root.join("config/routes/*.rb")]
  end
end
Enter fullscreen mode Exit fullscreen mode
# config/routes.rb
MyApp::Application.routes.draw do
  namespace :api do
    resources :users
    resources :projects
    resources :tasks
  end
end
Enter fullscreen mode Exit fullscreen mode
# config/routes/admin.rb
MyApp::Application.routes.draw do
  namespace :admin do
    resources :users
    resources :projects
    resources :tasks
  end
end
Enter fullscreen mode Exit fullscreen mode
# config/routes/admin_api.rb
MyApp::Application.routes.draw do
  namespace :admin do
    namespace :api do
      resources :users
      resources :projects
      resources :tasks
    end
  end
Enter fullscreen mode Exit fullscreen mode
# config/routes/api.rb
MyApp::Application.routes.draw do
  namespace :api do
    resources :users
    resources :projects
    resources :tasks
  end
end
Enter fullscreen mode Exit fullscreen mode
Collapse
vickodin profile image
Victor

Thanks, used your way.

P.S. In previous project used the next receipt = mattboldt.com/separate-rails-route... but your style is more clean, imho.

Collapse
buddyreno profile image
Buddy Reno Author

Dude that's great! I will re-evaluate my approach knowing this. :)