Rails extends routes searching to include dynamic URL params


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,

  resources :cats do
    resources :toys
  end

Now, if we search for rails routes -g cats/:cat_id/toys/:id, we get the following output,

  Prefix Verb   URI Pattern                      Controller#Action
  cat_toy GET    /cats/:cat_id/toys/:id(.:format) toys#show
          PATCH  /cats/:cat_id/toys/:id(.:format) toys#update
          PUT    /cats/:cat_id/toys/:id(.:format) toys#update
          DELETE /cats/:cat_id/toys/:id(.:format) toys#destroy

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,

  Prefix Verb   URI Pattern                      Controller#Action
  cat_toy GET    /cats/:cat_id/toys/:id(.:format) toys#show
          PATCH  /cats/:cat_id/toys/:id(.:format) toys#update
          PUT    /cats/:cat_id/toys/:id(.:format) toys#update
          DELETE /cats/:cat_id/toys/:id(.:format) toys#destroy

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.

  Prefix Verb URI Pattern                  Controller#Action
  cat_toys GET  /cats/:cat_id/toys(.:format) toys#index
         POST /cats/:cat_id/toys(.:format) toys#create

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.

  Prefix Verb   URI Pattern                      Controller#Action
  new_cat_toy GET    /cats/:cat_id/toys/new(.:format) toys#new
  cat_toy GET    /cats/:cat_id/toys/:id(.:format) toys#show
          PATCH  /cats/:cat_id/toys/:id(.:format) toys#update
          PUT    /cats/:cat_id/toys/:id(.:format) toys#update
          DELETE /cats/:cat_id/toys/:id(.:format) toys#destroy

Join Our Newsletter