How to See All Routes in Rails: Simple Guide
To see all routes in a Rails application, run the command
rails routes in your terminal. This command lists all defined routes with their HTTP methods, paths, and controller actions.Syntax
The basic command to list routes is rails routes. You can add options like -c CONTROLLER to filter routes by controller or --expanded to see detailed output.
rails routes: Lists all routes.-c CONTROLLER: Shows routes for a specific controller.--expanded: Displays routes with extra details.
bash
rails routes rails routes -c users rails routes --expanded
Example
This example shows how to list all routes in a Rails app. The output includes HTTP verbs, paths, controller#action, and route names.
bash
rails routes
Output
Prefix Verb URI Pattern Controller#Action
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
Common Pitfalls
Sometimes developers forget to run the command inside the Rails project directory, causing errors. Also, using rake routes is legacy; prefer rails routes in Rails 5 and later. Filtering routes incorrectly or missing the right controller name can show no results.
bash
Wrong: rake routes Right: rails routes Wrong filtering: rails routes -c wrongcontroller Right filtering: rails routes -c users
Quick Reference
| Command | Description |
|---|---|
| rails routes | List all routes |
| rails routes -c CONTROLLER | Filter routes by controller |
| rails routes --expanded | Show detailed route info |
| rails routes | grep keyword | Search routes by keyword |
Key Takeaways
Use
rails routes to list all routes in your Rails app.Run the command inside your Rails project folder to avoid errors.
Use
-c CONTROLLER to filter routes by controller name.Avoid using legacy
rake routes in modern Rails versions.Use
--expanded for more detailed route information.