Read more

How to split config/routes.rb in Rails 4

Dominik Schöler
August 13, 2015Software engineer at makandra GmbH

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

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

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
Posted by Dominik Schöler to makandra dev (2015-08-13 15:53)