Skip to content

RESTful Routes in Ruby on Rails — Resourceful CRUD Patterns

DodaTech Updated 2026-06-28 4 min read

In this tutorial, you will learn about RESTful Routes in Ruby on Rails. We cover key concepts, practical examples, and best practices to help you master this topic.

Rails RESTful routes map HTTP verbs and URLs to controller actions using the resources method, following REST principles for Create, Read, Update, and Delete operations.

What You'll Learn

By the end of this tutorial, you'll define resourceful routes, map CRUD to controller actions, use singular and nested resources, implement shallow nesting, and follow REST best practices.

Why RESTful Routes Matter

RESTful routing provides a predictable URL structure for web APIs. Every developer knows that GET /products lists products and POST /products creates one, following HTTP semantics.

Real-World Use

A Rails API defines resources for products, orders, and customers. Each resource maps to seven standard routes. Custom actions use member and collection routes to extend REST.

RESTful Path

flowchart LR
  A[Rails Routing] --> B[RESTful Routes]
  B --> C[HTTP Verbs]
  B --> D[Nesting]
  B --> E[Shallow]
  B --> F[Singular]
  B --> G{You Are Here}
  style G fill:#f90,color:#fff

Standard REST Routes

The seven RESTful actions mapped by resources.

Rails.application.routes.draw do
  resources :products
  # Generates these routes:
  # GET    /products          -> products#index
  # GET    /products/new      -> products#new
  # POST   /products          -> products#create
  # GET    /products/:id      -> products#show
  # GET    /products/:id/edit -> products#edit
  # PATCH  /products/:id      -> products#update
  # DELETE /products/:id      -> products#destroy
end

Custom REST Actions

Add member and collection actions.

Rails.application.routes.draw do
  resources :products do
    # Collection: no ID
    collection do
      get :search        # GET /products/search -> products#search
      post :import       # POST /products/import -> products#import
    end
    # Member: has ID
    member do
      patch :publish     # PATCH /products/:id/publish -> products#publish
      post :clone        # POST /products/:id/clone -> products#clone
    end
    # Shorthand
    get :search, on: :collection
    patch :publish, on: :member
  end
end

Nested Resources

Nest routes for parent-child relationships.

Rails.application.routes.draw do
  resources :products do
    resources :reviews, only: [:index, :new, :create]
    # Generates:
    # GET    /products/:product_id/reviews      -> reviews#index
    # GET    /products/:product_id/reviews/new  -> reviews#new
    # POST   /products/:product_id/reviews      -> reviews#create
  end
  # Deep nesting is discouraged
  # resources :products do
  #   resources :reviews do
  #     resources :comments  # Too deep!
  #   end
  # end
end

Shallow Nesting

Simplify deep nested routes with shallow.

Rails.application.routes.draw do
  resources :products, shallow: true do
    resources :reviews
    resources :images
  end
  # Equivalent to:
  resources :products do
    resources :reviews, only: [:index, :new, :create]
    resources :images, only: [:index, :new, :create]
  end
  resources :reviews, only: [:show, :edit, :update, :destroy]
  resources :images, only: [:show, :edit, :update, :destroy]
  # Result:
  # /products/1/reviews      -> reviews#index
  # /reviews/2              -> reviews#show (shallow)
end

Singular Resources

Routes for resources without IDs.

Rails.application.routes.draw do
  # User has one profile (no ID needed)
  resource :profile
  # Generates:
  # GET    /profile/new   -> profiles#new
  # POST   /profile       -> profiles#create
  # GET    /profile       -> profiles#show
  # GET    /profile/edit  -> profiles#edit
  # PATCH  /profile       -> profiles#update
  # DELETE /profile       -> profiles#destroy
end

Common Mistakes

1. Over-Nesting Routes

Three or more levels deep creates complex URLs. Use shallow nesting.

2. Exposing All Seven Routes Unnecessarily

resources :posts creates the destroy route. Use only: or except: for security.

3. Confusing New and Create Routes

new renders the form (GET). create processes the submission (POST).

4. Using GET for Destructive Actions

DELETE /products/:id uses DELETE verb. Never use GET for deletions.

5. Not Using member/collection for Custom Actions

Custom actions without proper routing go against REST conventions.

Practice Questions

1. What seven routes does resources generate?

index, new, create, show, edit, update, destroy.

2. What is the difference between nested and shallow routes?

Nested routes always include parent ID. Shallow routes only nest for index/new/create.

3. When do you use singular resource?

For Singleton resources like profile or settings that belong one-to-one with the current user.

4. How do you restrict resources to read-only?

resources :products, only: [:index, :show].

5. Challenge: Design RESTful routes for a blog with posts, comments, and likes.

resources :posts, shallow: true do
  resources :comments
  resources :likes, only: [:create, :destroy]
end

FAQ

What is the HTTP verb for update?

PATCH is preferred for partial updates. PUT replaces the entire resource.

Can I customize the URL path?

Yes. resources :posts, path: 'articles' maps to /articles.

What is the difference between new and create?

new renders the form. create processes the form submission.

How do I add multiple custom collection routes?

Define multiple routes inside the collection block.

Should I use shallow nesting for all resources?

Use shallow when nesting exceeds one level or parent references are redundant.

Mini Project: E-Commerce RESTful Routes

Design RESTful routes for an e-commerce application.

Rails.application.routes.draw do
  resources :products, shallow: true do
    resources :reviews, only: [:index, :new, :create]
    collection do
      get :search
      post :import
    end
    member do
      patch :publish
      patch :unpublish
    end
  end
  resource :cart, only: [:show, :update, :destroy] do
    post :add_item
    delete :remove_item
  end
  resources :orders, only: [:index, :show, :create]
end

What's Next

Rails Controllers Deep Rails Routing Deep Rails Params Strong Parameters

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro