rails_routes

Every HTTP route of the app, generated by the app itself with bin/rails routes --expanded, so engines and route concerns resolve exactly as Rails sees them.

  1. Parameters
  2. Example
  3. Engines and mounted apps
  4. How it runs
    1. The app’s Ruby
  5. Routes without booting
  6. When routes can’t be listed

Parameters

Parameter Type Description
filter string Optional. Case-insensitive substring matched against the route name, path, verb and controller#action

Example

// rails_routes { "filter": "comments" }
{
  "source": "routes_expanded.txt",
  "total": 7,
  "count": 2,
  "routes": [
    {
      "name": "post_comments",
      "verb": "POST",
      "path": "/posts/:post_id/comments(.:format)",
      "action": "create",
      "source_location": "config/routes.rb:3",
      "controller": "comments",
      "controller_action": "comments#create"
    },
    {
      "name": "post_comment",
      "verb": "DELETE",
      "path": "/posts/:post_id/comments/:id(.:format)",
      "action": "destroy",
      "source_location": "config/routes.rb:3",
      "controller": "comments",
      "controller_action": "comments#destroy"
    }
  ]
}

total is the number of routes in the app, and count is the number that matched the filter.

Engines and mounted apps

Routes of a mounted engine have paths relative to where the engine is mounted, so they come with engine and mounted_at. The mount itself is a route whose action is the engine or Rack app:

{ "name": "status_engine", "path": "/status", "action": "StatusEngine::Engine", "source_location": "config/routes.rb:18" },
{ "name": "sidekiq_web", "path": "/sidekiq", "action": "Sidekiq::Web", "source_location": "config/routes.rb:17" },
{
  "engine": "StatusEngine::Engine",
  "mounted_at": "/status",
  "name": "details",
  "verb": "GET",
  "path": "/details(.:format)",
  "action": "show",
  "controller": "status_engine/status",
  "controller_action": "status_engine/status#show"
}

How it runs

The tool runs bin/rails routes --expanded in --root with Bundler’s environment cleared, so the app loads its own Gemfile. The command times out after 60 seconds by default (RAILS_AGENT_KIT_ROUTES_TIMEOUT).

The app’s Ruby

The tool runs the app’s own Ruby. It reads the version the app pins from the first of .ruby-version, .tool-versions, mise.toml (or .mise.toml) and the Gemfile ruby directive, looking in --root and then its parent directories, as rbenv and mise do.

  • If the Ruby running rails-agent-kit matches, it’s used.
  • Otherwise the tool looks for a matching install of mise, rbenv (RBENV_ROOT), asdf (ASDF_DATA_DIR) or chruby (~/.rubies, /opt/rubies) and runs that Ruby directly. This also works when your editor starts the server without your shell profile, so version-manager shims aren’t on PATH.
  • If the pinned Ruby isn’t installed, the answer says so, for example “The app wants Ruby 3.3.5 (from .ruby-version), but it isn’t installed. Install it with rbenv install 3.3.5“, instead of a boot error.

The Ruby that was used is in the answer:

{
  "source": "/home/me/.rbenv/versions/3.3.5/bin/ruby bin/rails routes --expanded",
  "total": 7,
  "count": 7,
  "ruby": { "version": "3.3.5", "required": "3.3.5", "source": ".ruby-version", "manager": "rbenv",
            "command": "/home/me/.rbenv/versions/3.3.5/bin/ruby" },
  "routes": [ ... ]
}

RAILS_AGENT_KIT_ROUTES_COMMAND overrides all of this.

A relative --routes-file path is resolved against --root.

Routes without booting

If your app can’t boot locally (it needs a database, secrets or Docker), you have two options.

Point the server at a saved dump:

bin/rails routes --expanded > tmp/routes.txt
rails-agent-kit --root . --routes-file tmp/routes.txt

Run the routes command somewhere else, for example in your Docker Compose service:

RAILS_AGENT_KIT_ROUTES_COMMAND="docker compose exec -T web bin/rails routes --expanded" \
  rails-agent-kit --root /path/to/your/rails/app

When routes can’t be listed

If the command fails, times out or prints nothing it can parse, the tool doesn’t fail. It returns routes_unavailable: true, a message explaining why (with the tail of the error output) and how to fix it, and the raw config/routes.rb as routes_rb, so the agent still has something to read.

{
  "routes_unavailable": true,
  "message": "Could not list routes: ... Falling back to the raw config/routes.rb (no route expansion, helpers or engine routes). Tip: run `bin/rails routes --expanded > tmp/routes.txt` and start the server with --routes-file tmp/routes.txt.",
  "fallback_source": "config/routes.rb",
  "routes_rb": "Rails.application.routes.draw do\n  resources :posts do\n ..."
}