RoR RESTful Routes

REST (Representational State Transfer) is a convention for designing URLs that represent resources and use HTTP verbs to describe the action being performed. Rails is built around REST. Understanding it fully lets you design clean, predictable APIs and web interfaces that developers and tools can reason about immediately.

Resources and Actions

A resource is any noun in your app — articles, users, products, orders. Every resource supports up to seven standard actions.

REST Diagram for "articles" resource:

URL               HTTP Verb   Action    Purpose
------------------------------------------------------
/articles         GET         index     List all articles
/articles/new     GET         new       Show create form
/articles         POST        create    Save new article
/articles/:id     GET         show      Show one article
/articles/:id/edit GET        edit      Show edit form
/articles/:id     PATCH       update    Save changes
/articles/:id     DELETE      destroy   Remove article

One Line Creates All Seven

config/routes.rb

resources :articles

This single line generates all seven routes above. Rails maps the HTTP verb and URL path together to determine the action. The same URL /articles/5 responds differently depending on whether the verb is GET (show), PATCH (update), or DELETE (destroy).

How the Verb and URL Combine

GET  /articles/5  →  show the article with id=5
PATCH /articles/5 →  update the article with id=5
DELETE /articles/5 → delete the article with id=5

This is REST: the resource (article 5) stays the same, and the verb describes what to do with it.

Only and Except — Limit Routes

resources :articles, only: [:index, :show]
# Creates only GET /articles and GET /articles/:id

resources :products, except: [:destroy]
# Creates all routes except DELETE /products/:id

resources :settings, only: [:edit, :update]
# User can only edit their settings — no listing, showing, or deleting

Nested Resources

When one resource belongs to another, nest their routes to reflect the relationship in the URL:

resources :articles do
  resources :comments
end

This generates URLs that show the parent context:

GET    /articles/:article_id/comments          comments#index
GET    /articles/:article_id/comments/new      comments#new
POST   /articles/:article_id/comments          comments#create
GET    /articles/:article_id/comments/:id      comments#show
GET    /articles/:article_id/comments/:id/edit comments#edit
PATCH  /articles/:article_id/comments/:id      comments#update
DELETE /articles/:article_id/comments/:id      comments#destroy

In the comments controller, use params[:article_id] to find the parent article:

def index
  @article  = Article.find(params[:article_id])
  @comments = @article.comments
end

Shallow Nesting

Deep nesting creates long, awkward URLs. Use shallow: true to nest only where the parent ID is necessary:

resources :articles do
  resources :comments, shallow: true
end

Generates:
  GET /articles/:article_id/comments     ← needs parent (listing)
  POST /articles/:article_id/comments    ← needs parent (creating)
  GET /comments/:id                      ← no parent needed (reading one)
  PATCH /comments/:id                    ← no parent needed (editing)
  DELETE /comments/:id                   ← no parent needed (deleting)

Member and Collection Routes

Add custom routes to an existing resource without breaking its RESTful structure:

resources :articles do
  member do
    post :publish    ← acts on a specific article: POST /articles/:id/publish
    post :archive    ← POST /articles/:id/archive
  end

  collection do
    get :popular     ← acts on the collection: GET /articles/popular
    get :trending    ← GET /articles/trending
  end
end

Member routes need an :id. Collection routes act on the whole resource group.

Namespace Routes

Group routes under a namespace for administrative areas:

namespace :admin do
  resources :users
  resources :articles
  resources :settings, only: [:index, :update]
end

Generates URLs prefixed with /admin:

GET   /admin/users          admin/users#index
GET   /admin/users/:id      admin/users#show
GET   /admin/articles       admin/articles#index

Controllers live in app/controllers/admin/:

app/controllers/admin/users_controller.rb

class Admin::UsersController < ApplicationController
  before_action :require_admin!
  ...
end

Route URL Helpers

Route         Helper (_path)          Helper (_url)
articles      articles_path           articles_url
new article   new_article_path        new_article_url
article       article_path(@article)  article_url(@article)
edit article  edit_article_path(@a)   edit_article_url(@a)

Use _path helpers in views (relative URL). Use _url helpers in mailers and redirects where a full URL is needed.

See All Routes

rails routes

Filter by controller:
rails routes -c articles

Filter by URL:
rails routes | grep admin

Open interactive route browser (Rails 7+):
Visit http://localhost:3000/rails/info/routes in development

RESTful Design Benefits

BenefitHow REST Provides It
Predictable URLsEvery resource follows the same 7-action pattern
No URL naming debatesConvention defines the URL for every action
Works with HTTP verbsGET, POST, PATCH, DELETE do what they say
API compatibilitySame routes work for HTML and JSON responses
Tooling supportlink_to, form_with, and route helpers work automatically

RESTful routing is one of the most valuable conventions Rails provides. Once you understand it, you can look at any Rails route file and immediately know what every URL does and which controller handles it.

Leave a Comment

Your email address will not be published. Required fields are marked *