How to List and Filter Routes in Ruby on Rails

1 min read

You can list all the routes in your Rails application by running the bin/rails routes command in the terminal.

$ bin/rails routes
              Prefix  Verb   URI Pattern                    Controller#Action
              sidekiq        /sidekiq(.:format)             Sidekiq::Web
        sidekiq_admin        /admin                         Sidekiq::Admin
    new_user_session  GET    /users/sign_in(.:format)       devise/sessions#new
        user_session  POST   /users/sign_in(.:format)       devise/sessions#create
destroy_user_session  DELETE /users/sign_out(.:format)      devise/sessions#destroy
    new_user_password GET    /users/password/new(.:format)  devise/passwords#new

The output shows a table with four columns:

  1. Prefix: Name of the route
  2. Verb: HTTP Method such as GET, POST, etc.
  3. URI Pattern: URL pattern to match
  4. Controller#Action: Names of the controller class and action method

Filtering Routes

To filter the routes, you can use the grep option, by passing the -g flag. The following commands filters all the routes containing the term songs.

$ bin/rails routes -g songs
Prefix Verb URI Pattern               Controller#Action
 songs GET  /songs(/:genre)(.:format) songs#index {:genre=>"rock"}

View in a Browser

Alternatively, you could also find all the routes by visiting the /rails/info/routes path on a running Rails application.

rails routes info
rails routes info

That's a wrap. I hope you found this article helpful and you learned something new.

As always, if you have any questions or feedback, didn't understand something, or found a mistake, please leave a comment below or send me an email. I reply to all emails I get from developers, and I look forward to hearing from you.

If you'd like to receive future articles directly in your email, please subscribe to my blog. Your email is respected, never shared, rented, sold or spammed. If you're already a subscriber, thank you.