In Rails, how do you list an app's routes with bin/rails routes, and what do its -c, -g and --expanded options do?
answer
- Prefix, Verb, URI Pattern, Controller#Action
- listed in match order
- -c narrows by controller
- -g searches names, verbs, paths
- /rails/info/routes in development
basics
~20 sbin/rails routes prints every route in match order as Prefix, Verb, URI Pattern and Controller#Action. -c filters by controller, -g greps names, verbs, paths and actions (or a concrete URL), and --expanded prints one block per route.
solid answer
~40 s`bin/rails routes` boots the app and prints the route table in the order the router tries it: the **Prefix** (add `_path` or `_url` for the helper), the **Verb**, the **URI Pattern** and the **Controller#Action**, plus any constraints. `-c BikesController` (or `-c bikes`, or `-c Admin::BikesController`) keeps routes whose controller matches; the match is a partial regexp, so `-c bikes` also shows `admin/bikes`. `-g` takes a pattern and keeps routes whose name, verb, path, controller or action matches, and if you give it a real URL such as `-g /bikes/42` it also lists every route whose pattern would match that URL. `-E` / `--expanded` prints each route as a labelled block, with the source file and line in development. In the browser, development apps serve the same table at `/rails/info/routes`, with a search box.
code
bash · 6 linesbin/rails routes -c BikesController # one controller (partial match)
bin/rails routes -c admin/bikes # namespaced controller
bin/rails routes -g dock # names, verbs, paths, actions containing 'dock'
bin/rails routes -g /bikes/42 # every route whose pattern matches this URL
bin/rails routes -E -g /bikes/42 # same, one block per route with source line
bin/rails routes > tmp/routes-before.txt # snapshot to diff after a changego deeper
Recall the four columns, that the Prefix plus _path or _url is the helper name, and the -c and -g filters.
Explain that the listing is in match order, how -g also matches a concrete URL, and when -E and its source line help.
Use the table as a debugging and review tool: grep a real URL to find the winning route and diff snapshots around routes changes.
Make the route table part of review for large apps, so ordering mistakes and accidental public endpoints are caught before deploy.
## Reading the route table `bin/rails routes` boots the application (in development by default) and prints every route the router knows, **in the order it tries them**: ```bash $ bin/rails routes -c bikes Prefix Verb URI Pattern Controller#Action bikes GET /bikes(.:format) bikes#index POST /bikes(.:format) bikes#create new_bike GET /bikes/new(.:format) bikes#new edit_bike GET /bikes/:id/edit(.:format) bikes#edit bike GET /bikes/:id(.:format) bikes#show ``` What each column tells you: - **Prefix** is the route's name. Add `_path` or `_url` to get the helper (`edit_bike_path`). A blank prefix means the line shares the name above it, or has no name at all. - **Verb** is the HTTP method the route accepts. - **URI Pattern** shows segments (`:id`), globs (`*path`) and the optional format. - **Controller#Action** is the endpoint, followed by any constraints, such as `{id: /\d+/}`. A redirect route shows as `redirect(301, ...)` and a mounted app by its class. Routes defined by mounted engines appear in their own sections after the application's, headed `Routes for <EngineName>`. Rails' internal development routes (the `/rails/info` pages) are left out. ## The four options | Option | Short | What it does | |---|---|---| | `--controller` | `-c` | Keeps routes whose controller matches the name you give | | `--grep` | `-g` | Keeps routes whose name, verb, path, controller or action matches a pattern | | `--expanded` | `-E` | Prints one labelled block per route instead of the table | | `--unused` | `-u` | Lists routes with no controller, or no action and no template | Those four are the command's own options in Rails 8.1; there is no output-format switch. ## -c in detail `-c` accepts the class name or its underscored form: `-c BikesController`, `-c bikes`, `-c Admin::BikesController` and `-c admin/bikes` all work. Rails strips the `Controller` suffix, underscores the name and matches it as a **partial** regexp against each route's controller. That means `-c bikes` also lists `admin/bikes` and any other controller whose name contains the text, which is handy for an overview and noisy when you want one controller. ## -g in detail `-g` builds a regexp from your pattern and keeps a route when it matches any of: - the route **name** (`-g new_bike`), - the **verb** (`-g POST`), - the **path** pattern text (`-g docks`), - the **controller** or **action**. It also treats the pattern as a **concrete URL**: `bin/rails routes -g /bikes/42` lists every route whose pattern would match `/bikes/42`, such as `bike GET /bikes/:id(.:format)` together with the PATCH, PUT and DELETE rows for the same path. Since the table is in match order, the first row with the right verb is the route that will win, which makes `-g` with a real URL the quickest way to answer "which action handles this request?". ## --expanded Long paths wrap badly in a narrow terminal. `-E` prints each route as a block: ```bash --[ Route 5 ]------------------------------- Prefix | bike Verb | GET URI | /bikes/:id(.:format) Controller#Action | bikes#show Source Location | config/routes.rb:4 ``` In development Rails records where each route was declared, and the expanded view prints it as **Source Location**, which is the fastest way to find the line in a large or split routes file. ## /rails/info/routes In the **development** environment the same table is served in the browser at `/rails/info/routes`. The page notes that routes match in priority from top to bottom and has a search box that shows the routes matching a typed path. A development 404 page for an unmatched URL shows the table too. Neither exists in production, so the command is the tool on a server or in CI. ## Habits that pay off 1. Filter before reading: `-c` for a controller, `-g` with a real URL when debugging a request. 2. Use `-E` when lines wrap, or when you need the source line. 3. Read top to bottom; the order is the priority. 4. Pipe the output to a file in reviews to diff the route table before and after a routes change.
- In Rails, why does bin/rails routes -c bikes also list Admin::BikesController routes?The controller filter is turned into an unanchored regexp, `/bikes/`, and matched against each route's controller string. `admin/bikes` contains `bikes`, so it matches. Pass `-c admin/bikes` or use `-g` with a more specific pattern when you want a narrower list.
- Where can you see a Rails app's routes in a browser, and in which environment?In development, at `/rails/info/routes`, which renders the same table with a search box for paths. A development 404 for an unmatched URL also shows it. Production serves neither page, so on a server or in CI you run `bin/rails routes` instead.
- bin/rails routes sorts routes alphabetically by path
- -g only searches the URI Pattern column
- bin/rails routes -E filters routes by a regular expression
- /rails/info/routes is available in production for debugging
- A blank Prefix means the route is broken