Skip to content

Ror Routing

DodaTech 3 min read

title: Ruby on Rails Routing — Complete Guide to URL Routing description: 'Learn Ruby on Rails routing: resourceful routes, member/collection routes, nested resources, route constraints, path helpers, and custom route matching.' date: 2026-06-28 lastmod: 2026-06-28 weight: 14 tags: [backend, ror]


Ruby on Rails routing maps incoming HTTP requests to controller actions, supporting RESTful resources, nested routes, member/collection routes, and custom route matching.

## What You'll Learn

By the end of this tutorial, you'll configure resourceful routes, nest resources, add member and collection routes, use route constraints, generate URL helpers, and inspect routes.

## Real-World Use

An e-commerce site routes: resources :products (catalog), resources :cart (shopping cart), nested resources :cart_items inside :cart, and custom checkout collection route.

## Routing Learning Path

```mermaid
flowchart LR
  A[MVC] --> B[Routing]
  B --> C[Controllers]
  C --> D[Views]
  D --> E[Models]
  B --> F{You Are Here}
  style F fill:#f90,color:#fff

Resourceful Routes

# config/routes.rb
Rails.application.routes.draw do
  resources :posts
  # GET    /posts          posts#index
  # POST   /posts          posts#create
  # GET    /posts/new      posts#new
  # GET    /posts/:id/edit posts#edit
  # GET    /posts/:id      posts#show
  # PATCH  /posts/:id      posts#update
  # DELETE /posts/:id      posts#destroy
end

With Options

# Limit actions
resources :posts, only: [:index, :show]
resources :admin_posts, except: [:destroy]

# Custom param name
resources :posts, param: :slug
# /posts/my-article-slug (params[:slug])

# Custom controller
resources :posts, controller: "admin/posts"

# Custom path prefix
resources :posts, path: "articles"
# /articles instead of /posts

# Shallow nesting
resources :regions do
  resources :locations, shallow: true
  # /regions/1/locations, /locations/2
end

Member and Collection Routes

resources :posts do
  member do
    post :publish        # POST /posts/:id/publish -> posts#publish
    get :preview         # GET  /posts/:id/preview -> posts#preview
  end
  collection do
    get :search          # GET /posts/search -> posts#search
    get :drafts          # GET /posts/drafts -> posts#drafts
  end
end

Nested Resources

resources :posts do
  resources :comments          # /posts/:post_id/comments
  resources :likes, only: [:create, :destroy]
end
# POST   /posts/:post_id/comments     comments#create
# DELETE /posts/:post_id/comments/:id  comments#destroy

Route Constraints

# Format constraint
resources :posts, constraints: { id: /\d+/ }

# Subdomain constraint
constraints subdomain: "admin" do
  resources :admin_users
end

# Request-based constraint
constraints ->(req) { req.remote_ip == "127.0.0.1" } do
  resources :debug
end

Common Mistakes

1. Deep Nesting

More than 1 level deep nesting makes URLs complex. Keep nesting shallow (max 1 level).

2. Adding Non-RESTful Routes When Not Needed

Many "custom" actions are actually standard CRUD. Use resources with only/except before adding custom routes.

3. Duplicate Routes

Multiple routes matching the same URL cause conflicts. Use rake routes to verify.

4. Not Using Named Routes

Always use path helpers (posts_path, new_post_path) instead of hardcoded URLs.

5. Forgetting to Order Routes

Routes match in order. More specific routes must come before general ones.

Practice Questions

1. How many routes does resources :posts create?

7 routes: index, show, new, create, edit, update, destroy.

2. What is the difference between member and collection routes?

Member routes operate on a single resource (/posts/:id/publish). Collection routes operate on the collection (/posts/search).

3. How do you inspect all routes in an app?

Run rails routes in the terminal. Add --grep search to filter.

4. What is a shallow nested route?

Shallow nesting avoids deep URLs. /regions/1/locations uses region context, but /locations/2 doesn't.

5. Challenge: Create routes for a blog with posts, comments, and a search feature.

Rails.application.routes.draw do
  resources :posts do
    resources :comments, only: [:create, :destroy]
    collection { get :search }
    member { post :publish }
  end
  root "posts#index"
end

FAQ

What is the difference between GET and POST for routes?

GET fetches data (idempotent). POST creates/modifies data (non-idempotent). Rails generates the correct verb from the route definition.

Can I use route globbing?

Yes. match 'posts/*path' => 'posts#show' catches /posts/anything/here.

How do I redirect old routes to new ones?

Use redirect: get '/old/posts' => redirect('/posts').

What are concerns in routes?

Concerns extract reusable route groups: concern :commentable { resources :comments } then resources :posts, concerns: :commentable.

How do I handle root URL routing?

root 'posts#index' maps / to the posts index action.

Mini Project: Blog Routes

Configure complete routing for a blog with posts, comments, and categories.

Rails.application.routes.draw do
  resources :categories
  resources :posts do
    resources :comments, only: [:create, :destroy]
    collection { get :search }
  end
  root "posts#index"
end

What's Next

Ruby on Rails Controllers Ruby on Rails Views

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro