As your Rails application grows, the routes file can get quite large.
Rails provides two ways to search for routes, http://localhost:3000/rails/info/routes
and rails routes
.
The web interface makes it easy to search for routes,
but most devs prefer the command line interface.
Using rails routes
shows all the routes in your application,
and rails routes -g cats
finds all the routes that match the string cats
.
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 cats/:cat_id/toys/:id
instead.
This can be frustrating, especially if we’re not sure what the dynamic URL param is called.
Before
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.
After
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 index
and create
are not shown in the output.
This is because the rails routes
command only shows the routes that match the string cats/10/toys/15
exactly,
no fuzzy matching is allowed.
To search for the index
and 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 new
route.
Maybe this is something that can be improved in the future.