Routes

The Rails router recognizes URLs and dispatches them to a controller's action. It can also generate paths and URLs, avoiding the need to hardcode strings in your views.

Routes

When your Rails application receives an incoming request for:

it asks the router to match it to a controller action. If the first matching route is:

config/routes.rb

the request is dispatched to the posts controller's show action with { id: '17' } in params.

app/controllers/posts_controller.rb app/views/posts/show.html.erb

CRUD, Verbs, and Actions

Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your index, show, new, edit, create, update and destroy actions, a resourceful route declares them in a single line of code.

config/routes.rb
Prefix Verb URI Pattern Controller#Action
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format posts#destroy

Multiple Resources

or

Singular Resources


This resourceful route:


Prefix Verb URI Pattern Controller#Action
user POST /user(.:format) users#create
new_user GET /user/new(.:format) users#new
edit_user GET /user/edit(.:format) users#edit
GET /user(.:format) users#show
PATCH /user(.:format) users#update
PUT /user(.:format) users#update
DELETE /user(.:format) users#destroy

Controller Namespaces and Routing

Prefix Verb URI Pattern Controller#Action
admin_posts GET /admin/posts(.:format) admin/posts#index
POST /admin/posts(.:format) admin/posts#create
new_admin_post GET /admin/posts/new(.:format) admin/posts#new
edit_admin_post GET /admin/posts/:id/edit(.:format) admin/posts#edit
admin_post GET /admin/posts/:id(.:format) admin/posts#show
PATCH /admin/posts/:id(.:format) admin/posts#update
PUT /admin/posts/:id(.:format) admin/posts#update
DELETE /admin/posts/:id(.:format) admin/posts#destroy

Controller Namespaces and Routing

app/controllers/admin/posts_controller.rb
config/routes.rb

or for a single case

config/routes.rb

Route without a module prefix

config/routes.rb

or for a single case:

config/routes.rb
Prefix Verb URI Pattern Controller#Action
posts GET /admin/posts(.:format) posts#index
POST /admin/posts(.:format) posts#create
new_post GET /admin/posts/new(.:format) posts#new
edit_post GET /admin/posts/:id/edit(.:format) posts#edit
post GET /admin/posts/:id(.:format) posts#show
PATCH /admin/posts/:id(.:format) posts#update
PUT /admin/posts/:id(.:format) posts#update
DELETE /admin/posts/:id(.:format) posts#destroy

Nested Resources

app/models/user.rb app/models/post.rb config/routes.rb
Prefix Verb URI Pattern Controller#Action
user_posts GET /users/:user_id/posts(.:format) posts#index
POST /users/:user_id/posts(.:format) posts#create
new_user_post GET /users/:user_id/posts/new(.:format) posts#new
edit_user_post GET /users/:user_id/posts/:id/edit(.:format) posts#edit
user_post GET /users/:user_id/posts/:id(.:format) posts#show
PATCH /users/:user_id/posts/:id(.:format) posts#update
PUT /users/:user_id/posts/:id(.:format) posts#update
DELETE /users/:user_id/posts/:id(.:format) posts#destroy

Nested Resources

config/routes.rb

Shallow Nesting

config/routes.rb

There exists shorthand syntax to achieve just that, via the :shallow option:

config/routes.rb
Prefix Verb URI Pattern Controller#Action
post_comments GET /posts/:post_id/comments(.:format) comments#index
POST /posts/:post_id/comments(.:format) comments#create
new_post_comment GET /posts/:post_id/comments/new(.:format) comments#new
edit_comment GET /comments/:id/edit(.:format) comments#edit
comment GET /comments/:id(.:format) comments#show
PATCH /comments/:id(.:format) comments#update
PUT /comments/:id(.:format) comments#update
DELETE /comments/:id(.:format) comments#destroy
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy

Routing concerns

let's refactor it:

Creating Paths and URLs From Objects

link_to helper

Adding More RESTful Actions

or


Prefix Verb URI Pattern Controller#Action
preview_post GET /posts/:id/preview(.:format) posts#preview
search_posts GET /posts/search(.:format) posts#search
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy

Non-Resourceful Routes

Naming Routes

config/routes.rb

HTTP Verb Constraints

config/rputes.rb

Segment Constraints

or

Advanced Constraints

Route Globbing

Example:

Another example:

Redirection

Using Root

Shortcut

Specifying a ControllerNamed Helper

Prefix Verb URI Pattern Controller#Action
posts GET /posts(.:format) messages#index
POST /posts(.:format) messages#create
new_post GET /posts/new(.:format) messages#new
edit_post GET /posts/:id/edit(.:format) messages#edit
post GET /posts/:id(.:format) messages#show
PATCH /posts/:id(.:format) messages#update
PUT /posts/:id(.:format) messages#update
DELETE /posts/:id(.:format) messages#destroy

Overriding the Named Helpers

Prefix Verb URI Pattern Controller#Action
messages GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_message GET /posts/new(.:format) posts#new
edit_message GET /posts/:id/edit(.:format) posts#edit
message GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy

Restricting the Routes Created

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
posts GET /posts(.:format) posts#index
post GET /posts/:id(.:format) posts#show

Routing specs

Routing specs live in spec/routing.

spec/routing/users_spec.rb

/

#