Read more

Clean your Rails routes: grouping

Florian Leinsinger
August 27, 2020Software engineer at makandra GmbH

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

Illustration book lover

Growing Rails Applications in Practice

Check out our e-book. Learn to structure large Ruby on Rails codebases with the tools you already know and love.

  • Introduce design conventions for controllers and user-facing models
  • Create a system for growth
  • Build applications to last
Read more 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')]
Posted by Florian Leinsinger to makandra dev (2020-08-27 21:26)