How to Inspect Rails Routes from the Terminal

If you’ve worked on a legacy Rails project, then you’ve probably been tasked with inspecting, adding to and refactoring routes. Rails does a great job of making routing easy with its Rails magic. In keeping with the idea of separation of concerns, Rails keeps all routing logic in an aptly name ‘routes.rb’ file.

When a project first starts out, the amount of routes is small and the nesting of namespaces and collections vs. member resources is very clear and easy to follow. As the years go by, the routes file adds more resources and file bloat starts to take over. Where it may have been easy to trace an endpoint resource up its namespaces to the root of its path, now you may have to scroll 50 or more lines from your endpoint to find the next nest layer of namespace. It can be a nightmare.

To combat these issues, Rails provides a terminal command called ‘rails routes’. In this article we will look at this command and different ways to use it to query and better navigate a massive routes file.

What is the ‘rails routes’ Command?

The rails routes command is a cli command that outputs all available routes in your application. Along with the cli command, you can also access a list of your routes in the browser by navigating to “https://localhost:3000/rails/info/routes."

You will have four outputs for each route. These outputs are the route name (if it exists), the HTTP verb, the url pattern and the routing parameters (if they are used). These four pieces of information are very useful in determining the full route including namespaces, what the components of the URI are, the action that will be performed when the endpoint is hit and what kind of parameters can be expected.

This output is a lot easier to navigate than the ‘routes.rb’ file, but depending on the size of your app, you may still be drowning in data overload. We can add extra arguments to the rails routes command to get more specific and understandable sets of routes.

Searching with Grep

Developers who are already familiar with linux or bash may know the grep command, which makes searching for items by text easier. The grep command stands for global regular expression print and allows users to search for items matching parts of an input string.

This is useful for us as we may just want to find certain routes that have part of a namespace, or that act on a specific resource. To use the grep command we will type:

`rails routes | grep <input_text>`

This will return all routes that contain the <input_text> anywhere in their path.

Searching Based on Controllers

Another way to limit your search results is to search by controller. With the Rails MVC architecture, it makes sense to look at actions as a collection of methods of a controller. Therefore, seeing the paths for each controller action is helpful.

To view all controller paths we will use the following rails routes command:

`rails routes -c <controller_name>`

This will return all routes that link to the <controller_name> controller.


Rails magic can be wonderful and horrible. The magic that allows your app to operate with a nice separation of concerns routes.rb file is great. The reality of a massive routes file is not. However, I hope that after reading this article you are confident in using the `rails routes` command to query your routes! Let me know in the comments if there are other rails command tools that help in you in development!




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store