Routes are defined in config/routes.rb
. They are often defined as a group of related routes, using the resources
or resource
methods.
resources :users
creates the following seven routes, all mapping to actions of UsersController
:
get '/users', to: 'users#index'
post '/users', to: 'users#create'
get '/users/new', to: 'users#new'
get '/users/:id/edit', to: 'users#edit'
get '/users/:id', to: 'users#show'
patch/put '/users/:id', to: 'users#update'
delete '/users/:id', to: 'users#destroy'
Action names are shown after the #
in the to
parameter above. Methods with those same names must be defined in app/controllers/users_controller.rb
as follows:
class UsersController < ApplicationController
def index
end
def create
end
# continue with all the other methods…
end
You can limit the actions that gets generated with only
or except
:
resources :users, only: [:show]
resources :users, except: [:show, :index]
You can view all the routes of your application at any given time by running:
$ rake routes
$ rake routes
# OR
$ rails routes
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
To see only the routes that map to a particular controller:
$ rake routes -c static_pages
static_pages_home GET /static_pages/home(.:format) static_pages#home
static_pages_help GET /static_pages/help(.:format) static_pages#help
$ rake routes -c static_pages
static_pages_home GET /static_pages/home(.:format) static_pages#home
static_pages_help GET /static_pages/help(.:format) static_pages#help
# OR
$ rails routes -c static_pages
static_pages_home GET /static_pages/home(.:format) static_pages#home
static_pages_help GET /static_pages/help(.:format) static_pages#help
You can search through routes using the -g
option. This shows any route that partially matches the helper method name, the URL path or the HTTP verb:
$ rake routes -g new_user # Matches helper method
$ rake routes -g POST # Matches HTTP Verb POST
$ rake routes -g new_user # Matches helper method
$ rake routes -g POST # Matches HTTP Verb POST
# OR
$ rails routes -g new_user # Matches helper method
$ rails routes -g POST # Matches HTTP Verb POST
Additionally, when running rails
server in development mode, you can access a web page that shows all your routes with a search filter, matched in priority from top to bottom, at <hostname>/rails/info/routes
. It will look like this:
Helper | HTTP Verb | Path | Controller#Action |
---|---|---|---|
Path / Url | [ Path Match ] | ||
users_path | GET | /users(.:format) | users#index |
POST | /users(.:format) | users#create | |
new_user_path | GET | /users/new(.:format) | users#new |
edit_user_path | GET | /users/:id/edit(.:format) | users#edit |
user_path | GET | /users/:id(.:format) | users#show |
PATCH | /users/:id(.:format) | users#update | |
PUT | /users/:id(.:format) | users#update | |
DELETE | /users/:id(.:format) | users#destroy |
Routes can be declared available for only members (not collections) using the method resource
instead of resources
in routes.rb
. With resource
, an index
route is not created by default, but only when explicitly asking for one like this:
resource :orders, only: [:index, :create, :show]