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 web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
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)