Clean your Rails routes: grouping

In Ruby on Rails, all the routes of a given application can be found within the config/routes.rb file.
You add more and more routes in this file as your project grows.

The problem here is that this file potentially becomes very complicated to manage over the time.
That’s why it’s important to find a way to order and maintain your routes.

See: Clean your Rails routes: grouping Show archive.org snapshot

Sometimes the routes.rb grows very fast and each line adds more confusion to it. Maybe it is time for a new approach.

The quoted article suggests to split up your routes.rb into small partials to keep it clean. For example you can create a own file for each namespace and separately.

config/routes
  ├── backend.rb
  └── frontend.rb

Then you have to configure the paths in your config/application.rb:

config.paths['config/routes.rb'] = Dir[Rails.root.join('config/routes/*.rb')]
Florian Leinsinger Over 3 years ago