Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split routes.rb into smaller files

Is it possible to split Rails 3.X routes.rb file?

We have so many resources it is difficult to find them. I would like to split at least APP and REST API routes.

Thanks!

like image 400
lzap Avatar asked Jan 18 '12 08:01

lzap


3 Answers

You can do that:

routes.rb

require 'application_routes'
require 'rest_api_routes'

lib/application_routes.rb

YourApplication::Application.routes.draw do
  # Application related routes
end

lib/rest_api_routes.rb

YourApplication::Application.routes.draw do
  # REST API related routes
end

UPDATE: (This method has since been removed from Rails)

Rails edge just got a great addition, multiple route files:

# config/routes.rb
draw :admin

# config/routes/admin.rb
namespace :admin do
  resources :posts
end

This will come handy for breaking down complex route files in large apps.

like image 182
Arun Kumar Arjunan Avatar answered Oct 31 '22 23:10

Arun Kumar Arjunan


In Rails3, you can set the configs in config/application.rb

config.paths.config.routes.concat Dir[Rails.root.join("config/routes/*.rb")]
like image 39
Marko Tunjic Avatar answered Oct 31 '22 23:10

Marko Tunjic


Rails 3.2.11

config.paths["config/routes"].concat Dir[Rails.root.join("config/routes/*.rb")]

like image 3
zuba Avatar answered Oct 31 '22 23:10

zuba