Rails Routes expanded view option

Listing rails routes and interpreting is quite troublesome if you are using a small screen on terminal. Rails 6 adds support to show rails routes in an expanded format with --expanded option.

Before Rails 6

Rails routes list all the routes defined for a Rails application with command given below.

rails routes

This lists down prefix, URI pattern, Controller information of each route in a table format.

    new_rails_conductor_inbound_email GET        /rails/conductor/action_mailbox/inbound_emails/new(.:format)                             rails/conductor/action_mailbox/inbound_emails#new
   edit_rails_conductor_inbound_email GET        /rails/conductor/action_mailbox/inbound_emails/:id/edit(.:format)                        rails/conductor/action_mailbox/inbound_emails#edit
        rails_conductor_inbound_email GET        /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                             rails/conductor/action_mailbox/inbound_emails#show
                                      PATCH      /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                             rails/conductor/action_mailbox/inbound_emails#update
                                      PUT        /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                             rails/conductor/action_mailbox/inbound_emails#update
                                      DELETE     /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                             rails/conductor/action_mailbox/inbound_emails#destroy
rails_conductor_inbound_email_reroute POST       /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format)                      rails/conductor/action_mailbox/reroutes#create

Each line of information goes to next line if you are using a small terminal screen. We have to track a row to interpret prefix, URI pattern or controller of certain row to interpret information. Thus, it is quite difficult to interpret such information.

Rails Routes Expanded option

Rails 6 introduces expanded option to be able to list down rails routes in an expanded format where it gets easy to interpret information of a certain route.

This is motivated based on a PSQL expand option (-x) to expand output.

To view Rails routes in an expanded format, pass --expanded option to rails routes command as given below.

rails routes --expanded

This will list down routes as given below. List same routes shown in the post above.

--[ Route 114 ]---------------------------------------------------------------------------
Prefix            | new_rails_conductor_inbound_email
Verb              | GET
URI               | /rails/conductor/action_mailbox/inbound_emails/new(.:format)
Controller#Action | rails/conductor/action_mailbox/inbound_emails#new
--[ Route 115 ]---------------------------------------------------------------------------
Prefix            | edit_rails_conductor_inbound_email
Verb              | GET
URI               | /rails/conductor/action_mailbox/inbound_emails/:id/edit(.:format)
Controller#Action | rails/conductor/action_mailbox/inbound_emails#edit
--[ Route 116 ]---------------------------------------------------------------------------
Prefix            | rails_conductor_inbound_email
Verb              | GET
URI               | /rails/conductor/action_mailbox/inbound_emails/:id(.:format)
Controller#Action | rails/conductor/action_mailbox/inbound_emails#show
--[ Route 117 ]---------------------------------------------------------------------------
Prefix            |
Verb              | PATCH
URI               | /rails/conductor/action_mailbox/inbound_emails/:id(.:format)
Controller#Action | rails/conductor/action_mailbox/inbound_emails#update
--[ Route 118 ]---------------------------------------------------------------------------
Prefix            |
Verb              | PUT
URI               | /rails/conductor/action_mailbox/inbound_emails/:id(.:format)
Controller#Action | rails/conductor/action_mailbox/inbound_emails#update
--[ Route 119 ]---------------------------------------------------------------------------
Prefix            |
Verb              | DELETE
URI               | /rails/conductor/action_mailbox/inbound_emails/:id(.:format)
Controller#Action | rails/conductor/action_mailbox/inbound_emails#destroy
--[ Route 120 ]---------------------------------------------------------------------------
Prefix            | rails_conductor_inbound_email_reroute
Verb              | POST
URI               | /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format)
Controller#Action | rails/conductor/action_mailbox/reroutes#create

These routes also show a route number. It’s just a number that indicates ordering in which these routes are defined along with prefix, verb, URI and controller#action.

As we can see, expanded option on rails routes outputs information in a table format which is much more readable and easy to follow than earlier version.

References

akshay

Akshay Mohite

Hi there! I am a Ruby on Rails & ReactJS Enthusiast, building some cool products at DTree Labs.

Read More
Buy me a coffee