Rails, Routes, & Flags

Published: 1/3/2020

Most of us have looked up routes from the command line.

$ rails routes
Prefix Verb URI Pattern Controller#Action
root GET / welcome#index
...

There can be hundreds of routes if it's a huge application. When looking for something specific I would default to

$ rails routes | grep welcome
root GET / welcome#index

A while ago I saw a person use rails routes -c welcome and I was like Wait! What?!

$ rails routes -c welcome
Prefix Verb URI Pattern Controller#Action
root GET / welcome#index

The -c & -g flag is available in rails > 5.0
The -E & -h flag is available in rails > 6.0

in a Rails 6 we can use a help flag.

$ rails routes -h
Usage:
rails routes [options]
Options:
-c, [--controller=CONTROLLER] # Filter by a specific controller, e.g. PostsController or Admin::PostsController.
-g, [--grep=GREP] # Grep routes by a specific pattern.
-E, [--expanded], [--no-expanded] # Print routes expanded vertically with parts explained.

Now if you want to see all your routes for Admin Posts

$ rails routes -c admin::post
Prefix Verb URI Pattern Controller#Action
admin_posts GET /admin/posts(.:format) admin/posts#index
POST /admin/posts(.:format) admin/posts#create
new_admin_post GET /admin/posts/new(.:format) admin/posts#new
edit_admin_post GET /admin/posts/:id/edit(.:format) admin/posts#edit
admin_post GET /admin/posts/:id(.:format) admin/posts#show
PATCH /admin/posts/:id(.:format) admin/posts#update
PUT /admin/posts/:id(.:format) admin/posts#update
DELETE /admin/posts/:id(.:format) admin/posts#destroy

So nice.