How to split config/routes.rb in Rails 4

Posted Almost 9 years ago. Visible to the public.

A word of caution

There should rarely be a reason for you to split up config/routes.rb. If you need to, probably your whole application should be split up.

Split it anyway

Rails::Engine looks at config.paths['config/routes.rb'] and registers its value with app.routes_reloader. This means you could put routing files anywhere and then require them. However, I recommend to put any routing files into config/routes/:

# config/routes/example.rb

Rails.application.routes.draw do
 resources :example
end

After creating your routing files, tell Rails how to find them:

# in config/application.rb

# Rails::Engine will register these files with app.routes_reloader
config.paths['config/routes.rb'] = Dir[Rails.root.join('config/routes/*.rb')]

Note that this will neglect your old config/routes.rb (which by now should be empty, anyway). However, you might want to leave a note to other developers:

# config/routes.rb

# See config/routes/*.rb
Dominik Schöler
Last edit
Almost 9 years ago
Dominik Schöler
License
Source code in this card is licensed under the MIT License.
Posted by Dominik Schöler to makandra dev (2015-08-13 13:53)