This blog is part of our Rails 5 series.

rails routes shows all the routes in the application.

$ 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 .

$ 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 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 .

# 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 pattern matching. This results in any routes that partially matches Prefix, Controller#Action or the URI pattern.

# 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.