As your Rails application grows, the routes file can get quite large.
Rails provides two ways to search for routes,
The web interface makes it easy to search for routes,
but most devs prefer the command line interface.
rails routes shows all the routes in your application,
rails routes -g cats finds all the routes that match the string
However, it can be difficult to find the route one is looking for on the command line,
especially, if the route contains dynamic URL params.
For example,to search for the route that matches
cats/10/toys/15, one has to search for
This can be frustrating, especially if we’re not sure what the dynamic URL param is called.
Let’s consider a routes file that looks like this,
Now, if we search for
rails routes -g cats/:cat_id/toys/:id,
we get the following output,
However searching for
rails routes -g cats/10/toys/15 returns no results.
Thanks to this PR,
Rails now supports searching for dynamic URL params in the
rails routes command, making it easier to find the route one is looking for.
Now if we search for
rails routes -g cats/10/toys/15, we get the following output,
Notice that the options
create are not shown in the output.
This is because the
rails routes command only shows the routes that match the string
no fuzzy matching is allowed.
To search for the
create routes, we can search for
rails routes -g cats/10/toys.
Funny enough, searching for
rails routes -g cats/10/toys/new only does a regex match
and does not show only the
Maybe this is something that can be improved in the future.