---
title: "Rails 5 improves route search with advanced options"
description:
  "Rails 5 brings additional options for rake routes task to display specific
  routes."
canonical_url: "https://www.bigbinary.com/blog/rails-5-options-for-rake-routes"
markdown_url: "https://www.bigbinary.com/blog/rails-5-options-for-rake-routes.md"
---

# Rails 5 improves route search with advanced options

Rails 5 brings additional options for rake routes task to display specific
routes.

- Author: Abhishek Jain
- Published: February 16, 2016
- Categories: Rails 5, Rails

`rails routes` shows all the routes in the application.

```bash

$ rake routes

Prefix       Verb   URI Pattern                   Controller#Action
wishlist_user GET    /users/:id/wishlist(.:format) users#wishlist
        users GET    /users(.:format)              users#index
              POST   /users(.:format)              users#create
     new_user GET    /users/new(.:format)          users#new
    edit_user GET    /users/:id/edit(.:format)     users#edit
         user GET    /users/:id(.:format)          users#show
              PATCH  /users/:id(.:format)          users#update
              PUT    /users/:id(.:format)          users#update
              DELETE /users/:id(.:format)          users#destroy
     products GET    /products(.:format)           products#index
              POST   /products(.:format)           products#create
and so on ......

```

This list can be lengthy and it could be difficult to locate exactly what user
is looking for.

## Ways to search specific routes prior to Rails 5

To see only specific routes we can use commands like `grep`.

```bash

$ rake routes | grep products
Prefix       Verb   URI Pattern                   Controller#Action
products      GET    /products(.:format)           products#index
              POST   /products(.:format)           products#create

```

## Options with Rails 5

Rails 5 [has added](https://github.com/rails/rails/pull/23225) options in
`rails routes` to perform pattern matching on routes.

#### Controller specific search

Use option `-c` to search for routes related to controller. Also remember that
Rails does case insensitive search. So `rails routes -c users` is same as
`rails routes -c Users`.

```bash

# Search for Controller name
$ rails routes -c users
       Prefix Verb   URI Pattern                   Controller#Action
wishlist_user GET    /users/:id/wishlist(.:format) users#wishlist
        users GET    /users(.:format)              users#index
              POST   /users(.:format)              users#create

# Search for namespaced Controller name.
$ rails routes -c admin/users
         Prefix Verb   URI Pattern                     Controller#Action
    admin_users GET    /admin/users(.:format)          admin/users#index
                POST   /admin/users(.:format)          admin/users#create

# Search for namespaced Controller name.
$ rails routes -c Admin::UsersController
         Prefix Verb   URI Pattern                     Controller#Action
    admin_users GET    /admin/users(.:format)          admin/users#index
                POST   /admin/users(.:format)          admin/users#create

```

#### Pattern specific search

Use `-g` option to do
[general purpose](https://github.com/rails/rails/pull/23611) pattern matching.
This results in any routes that partially matches Prefix, Controller#Action or
the URI pattern.

```bash

# Search with pattern
$ rails routes -g wishlist
       Prefix Verb URI Pattern                   Controller#Action
wishlist_user GET  /users/:id/wishlist(.:format) users#wishlist

# Search with HTTP Verb
$ rails routes -g POST
    Prefix Verb URI Pattern            Controller#Action
           POST /users(.:format)       users#create
           POST /admin/users(.:format) admin/users#create
           POST /products(.:format)    products#create

# Search with URI pattern
$ rails routes -g admin
       Prefix Verb   URI Pattern                     Controller#Action
  admin_users GET    /admin/users(.:format)          admin/users#index
              POST   /admin/users(.:format)          admin/users#create

```

Note that using CONTROLLER=some_controller has now been deprecated. This had the
same effect as searching for a controller specific route.

## Links

- [Human page](https://www.bigbinary.com/blog/rails-5-options-for-rake-routes)
