Filter rails routes easily with FZF

In my last two jobs, I've been working on pretty big Ruby on Rails applications. Over time and despite our best efforts, interacting with the application through the built-in commands becomes slower. This is especially sensible for commands such as rails routes that I use frequently. But searching the rails routes output does not have to be painful: I have a tip for this that I shared with many colleagues over the years, and I thought it was worth sharing here. It involves a neat command-line utility called fzf.

Rails routes?

rails routes allows one to see all routes defined in a Ruby on Rails application. The output contains all the URLs, the controller and action on which they're mapped and the name of the corresponding route helper. A growing or mature Rails project easily reaches hundreds or thousands of line in the output of this command.

When you need to dig into an existing feature or to investigate a bug, starting from the queries you see in your browser network inspector, then finding the matching controller is a mandatory first step and you may need to launch rails routes for this.

rails routes itself does provide a few options to filter its output:

1
2
3
4
5
6
7
Usage:
  rails routes [options]

Options:
  -c, [--controller=CONTROLLER]      # Filter by a specific controller, e.g. PostsController or Admin::PostsController.
  -g, [--grep=GREP]                  # Grep routes by a specific pattern.
  -E, [--expanded], [--no-expanded]  # Print routes expanded vertically with parts explained.

But to be honest, I never really managed to use them (especially on applications with namespaces and nested routes, never sure what values to pass). I generally turned to grep, using rails routes | grep pattern to only show the relevant routes. But as discussed, if rails routes is slow and your initial search fails, you need to relaunch another rails routes, and another one, and it becomes frustrating pretty quickly.

Therefore, in the same spirit of using grep on the output, I turned to another tool for this: fzf.

What is FZF?

FZF is a command-line fuzzy finder that, given some text, offers you a prompt allowing you to filter the content on the fly. Allowing you to type something, see what matches, delete or reword your search, type something else, complete it, etc. This is the tool powering my file-search in vim via the fzf.vim plugin, it is both very reliable and extremely fast, and it works wonder for rails routes, as you can see:

A recording of my terminal where I filter a dummy project rails routes with FZF
Filtering a dummy project rails routes with FZF

Make sure to look into the project readme and wiki for more ideas: if you spend some time in your terminal, I'm sure you'll find many more use-cases for this great tool.

By default, you'll be in fuzzy search mode: you'll see results matching all letters from your search in the same order, no matter if they're adjacent or not. That's a good start but sometimes you'll want to be more precise than that.

  • You want to find an exact pattern? Prefix your search with a single quote, and 'pattern will only show results containing exactly "pattern".
  • In the contrary, do you need to exclude some words? Prefix your search with an exclamation point: !pattern will return all lines that don't contain "pattern".

There are a few other search syntaxes but these 2 are clearly the most useful ones. And you can combine them, meaning you can search for 'account !admin to find, for instance, all routes containing exactly account but not the ones containing admin. That should be enough to cover most searches you'll be doing, and definitely enough for searching through your rails routes!

Happy searching 🕵️‍♂️