LinuxCommandLibrary

rails-routes

Display application's defined routes

TLDR

List all routes

$ rails routes
copy

List all routes in an expanded format
$ rails routes [[-E|--expanded]]
copy

List routes partially matching URL helper method name, HTTP verb, or URL path
$ rails routes [[-g|--grep]] [posts_path|GET|/posts]
copy

List routes that map to a specified controller
$ rails routes [[-c|--controller]] [posts|Posts|Blogs::PostsController]
copy

SYNOPSIS

rails routes [options]

PARAMETERS

--help, -h
    Displays a help message outlining available options and usage.

--grep=PATTERN, -g=PATTERN
    Filters the displayed routes by a given PATTERN. The pattern can match the route's prefix, verb, URI pattern, or controller#action.

-E
    When used with --grep, treats the provided PATTERN as a regular expression, allowing for more powerful filtering.

--expanded, -x
    Outputs each route's information on separate lines, providing a more detailed and readable view for complex routes.

--json
    Outputs the routes in JSON format, useful for programmatic parsing or integration with other tools.

--controller=CONTROLLER
    Filters routes to only show those handled by a specific CONTROLLER (e.g., UsersController).

DESCRIPTION

The rails routes command displays a comprehensive list of all defined routes within a Ruby on Rails application. This includes the HTTP verb (GET, POST, PUT, DELETE, PATCH), the URI pattern, the corresponding controller action, and the route's helper name (prefix). It is an indispensable tool for debugging routing issues, understanding the application's URL structure, and verifying that routes are correctly defined and mapped.

The command is typically executed from the root directory of a Rails application, leveraging its configuration to present the current routing table. It helps developers quickly ascertain how incoming requests are dispatched to specific parts of their application.

CAVEATS

The rails routes command must be executed within the root directory of a valid Ruby on Rails application. It requires Ruby and the Rails framework to be installed in the environment. The output reflects only the routes defined in the application's config/routes.rb file and does not validate their accessibility or the existence of corresponding controller actions (unless --unused is specifically requested).

OUTPUT COLUMNS

The default output of rails routes typically presents routes in four columns:
1. Prefix: The helper method name used to generate URLs for the route.
2. Verb: The HTTP method(s) (e.g., GET, POST) that the route responds to.
3. URI Pattern: The path that matches the route, potentially including dynamic segments.
4. Controller#Action: The controller and action method that handles requests matching this route.

ROUTE ORDER

In Rails, the order in which routes are defined in config/routes.rb is significant. Rails matches incoming requests against routes in the order they appear. More specific routes should generally be defined before more generic ones to ensure they are matched correctly.

HISTORY

The ability to inspect application routes has been a core feature of Ruby on Rails since its early days. Initially available as a Rake task called rake routes, it was later integrated directly into the rails command-line interface (CLI) as rails routes around Rails 4.0. This unification streamlined the developer workflow by consolidating many common tasks under the primary rails executable.

SEE ALSO

rails console, rails server, rake routes, grep(1)

Copied to clipboard