My Knowledge Wiki
  • Introduction
  • My Stack
  • Sharing
  • Work
    • How I Want to Work
    • Hiring Process
  • Apps & Tools
    • Raycast
      • Extensions
    • Visual Studio Code
      • Theme
      • Extensions
    • Google Chrome
      • Theme
      • Extensions
    • iTerm 2
      • Theme
    • Fish
      • Plugins
    • Docker
      • Volumes
        • Mounting
    • Web Bundlers
      • Webpack
  • DevOps
    • Databases
      • Estimating Connections
    • Kubernetes
      • Terminology
      • kubectl Cheatsheet
      • Best Practices
      • Application Health Checks
      • Upgrades
      • Troubleshooting
      • Ruby on Rails
  • Awesome Products
  • Engineering Management
    • Overview
  • Software Architecture
    • Microservices
    • Event sourcing
    • Serverless
    • Centralized Authentication
  • Talks
    • Software Architecture
      • An Insider's Look at the Technology That Powers Shopify
      • Building Extensible Platforms
  • Documentation
  • Machine Learning
    • Terminology
    • Regression
    • Overfitting
  • Programming Languages
    • Constructs
    • Go
      • Syntax
    • Ruby
      • Ruby on Rails
        • Setup
        • Gotchas
        • Helpers
        • Libraries
        • Routing
        • Status Code Symbols
      • Debugging
        • Byebug
          • Cheatsheet
      • Libraries
      • Lazy Enumerators
      • Snippets
  • Version Control
    • Git
      • Conventions
        • Conventional Commits
        • gitmoji
  • Education
    • Programming & Computer Science
      • Courses
    • Design
      • Courses and Books
    • Frontend Development
  • HTTP
    • Status Codes
  • Design
    • Icons
      • Icon Sets
  • Arabic Content | محتوى عربي
    • Learning
      • مصادر لتعلم البرمجة Ùˆ علوم الحاسب
    • Advice
      • إدارة المطورين: نبذة
      • نصائح تصحيح معتقدات خاطئة أمنيات للمبرمجين الجدد
  • Biology
    • Species
      • Ants
Powered by GitBook
On this page
  • Quick Cheatsheet
  • Resources
  • Member and Collection
  • Options
  • Single Resource
  • Matching
  • Redirect
  • Named Routes
  • Scopes
  • Nested Resources (routes)
  • Splitting Up Big Routes Files
  1. Programming Languages
  2. Ruby
  3. Ruby on Rails

Routing

PreviousLibrariesNextStatus Code Symbols

Last updated 5 years ago

Quick Cheatsheet

Mostly taken from .

Resources

resources :books

# BooksController:
# index  =>    GET /books
# new    =>    GET /books/new
# create =>   POST /books/new
# show   =>    GET /books/:id
# edit   =>    GET /books/:id/edit
# update =>    PUT /books/:id
# delete => DELETE /books/:id
#
# Helpers:
# new_book_path
# book_path(id)
# edit_book_path(id)

Member and Collection

collection is for routes on the collection.

member is for routes on a specific member.

Rails.applications.routes.draw do
  resources :events do
    collection do
        post :validate # localhost:3000/events/validate
    end

    member do
      post :publish # localhost:3000/events/1/publish
    end
end

Options

resources :photos,
  path_names: { new: 'brand_new' }    # /photos/1/brand_new
  path: 'postings'                    # /postings
  only: :index
  only: [:index, :show]
  except: :show
  except: [:index, :show]

  shallow: true                       # also generate shallow routes
  shalow_path: 'secret'
  shallow_prefix: 'secret'

Single Resource

resource :coder

# CodersController:
# new    =>    GET /coder/new
# create =>   POST /coder/new
# show   =>    GET /coder
# edit   =>    GET /coder/edit
# update =>    PUT /coder
# delete => DELETE /coder

Matching

match 'photo/:id' => 'photos#show'  # /photo/what-is-it
match 'photo/:id', id: /[0-9]+/     # /photo/0192
match 'photo/:id' => 'photos#show', constraints: { id: /[0-9]+/ }
match 'photo/:id', via: :get
match 'photo/:id', via: [:get, :post]

match 'photo/*path' => 'photos#unknown'    # /photo/what/ever

# params[:format] == 'jpg'
match 'photos/:id' => 'photos#show', :defaults => { :format => 'jpg' }

Redirect

match '/stories' => redirect('/posts')
match '/stories/:name' => redirect('/posts/%{name}')

Named Routes

# logout_path
match 'exit' => 'sessions#destroy', as: :logout

Scopes

scope 'admin', constraints: { subdomain: 'admin' } do
  resources ...
end

Nested Resources (routes)

Assuming an event has many registrations and we want registration routes to be nested under an event, e.g. localhost:3000/events/1/registrations, we can do:

Rails.applications.routes.draw do
  resources :events do
    resources :registrations
  end
end

Splitting Up Big Routes Files

First you have to make a new draw method into Rails's routing mapper via an initializer

# config/initializers/routing_draw.rb

# Adds draw method into Rails routing
# It allows us to keep routing splitted into files
class ActionDispatch::Routing::Mapper
  def draw(routes_name)
    instance_eval(File.read(Rails.root.join("config/routes/#{routes_name}.rb")))
  end
end

Update your config/routes.rb with the names of files in config/routes/*.rb

# config/routes.rb
MyApp::Application.routes.draw do
  draw :api_v1
  draw :api_v2
  draw :admin
end

New route files

# config/routes/api_v1.rb
namespace :api_v1 do
  # lots of routes
end

# config/routes/api_v2.rb
namespace :api_v2 do
  # lots of routes
end

# config/routes/admin.rb
namespace :admin do
  # lots of routes
end

(Mostly taken from )

are also a great example.

devhints
Matt Boldt's blog post
GitLab's route files