Rails 5 improves route search with advanced options

Abhishek Jain

Abhishek Jain

February 16, 2016

This blog is part of our  Rails 5 series.

rails routes shows all the routes in the application.

1
2$ rake routes
3
4Prefix       Verb   URI Pattern                   Controller#Action
5wishlist_user GET    /users/:id/wishlist(.:format) users#wishlist
6        users GET    /users(.:format)              users#index
7              POST   /users(.:format)              users#create
8     new_user GET    /users/new(.:format)          users#new
9    edit_user GET    /users/:id/edit(.:format)     users#edit
10         user GET    /users/:id(.:format)          users#show
11              PATCH  /users/:id(.:format)          users#update
12              PUT    /users/:id(.:format)          users#update
13              DELETE /users/:id(.:format)          users#destroy
14     products GET    /products(.:format)           products#index
15              POST   /products(.:format)           products#create
16and so on ......
17

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.

1
2$ rake routes | grep products
3Prefix       Verb   URI Pattern                   Controller#Action
4products      GET    /products(.:format)           products#index
5              POST   /products(.:format)           products#create
6

Options with Rails 5

Rails 5 has added options in rails routes to perform pattern matching on routes.

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.

1
2# Search for Controller name
3$ rails routes -c users
4       Prefix Verb   URI Pattern                   Controller#Action
5wishlist_user GET    /users/:id/wishlist(.:format) users#wishlist
6        users GET    /users(.:format)              users#index
7              POST   /users(.:format)              users#create
8
9# Search for namespaced Controller name.
10$ rails routes -c admin/users
11         Prefix Verb   URI Pattern                     Controller#Action
12    admin_users GET    /admin/users(.:format)          admin/users#index
13                POST   /admin/users(.:format)          admin/users#create
14
15# Search for namespaced Controller name.
16$ rails routes -c Admin::UsersController
17         Prefix Verb   URI Pattern                     Controller#Action
18    admin_users GET    /admin/users(.:format)          admin/users#index
19                POST   /admin/users(.:format)          admin/users#create
20

Use -g option to do general purpose pattern matching. This results in any routes that partially matches Prefix, Controller#Action or the URI pattern.

1
2# Search with pattern
3$ rails routes -g wishlist
4       Prefix Verb URI Pattern                   Controller#Action
5wishlist_user GET  /users/:id/wishlist(.:format) users#wishlist
6
7# Search with HTTP Verb
8$ rails routes -g POST
9    Prefix Verb URI Pattern            Controller#Action
10           POST /users(.:format)       users#create
11           POST /admin/users(.:format) admin/users#create
12           POST /products(.:format)    products#create
13
14# Search with URI pattern
15$ rails routes -g admin
16       Prefix Verb   URI Pattern                     Controller#Action
17  admin_users GET    /admin/users(.:format)          admin/users#index
18              POST   /admin/users(.:format)          admin/users#create
19

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

If this blog was helpful, check out our full blog archive.

Stay up to date with our blogs.

Subscribe to receive email notifications for new blog posts.