🏃

Railsのルーティング(routes)書き方 早見表

2022/10/22に公開

index 以外の action は省略しています。

https://railsguides.jp/routing.html

nested resources

  • Prefix: ○(単数形)
  • Path: ○
  • Id: ○
  • Controller: ○

ネストしたリソース

resources :posts do
  resources :comments
end
HTTP Verb Prefix URI Pattern Controller#Action
GET post_comments /posts/:post_id/comments comments#index

namespace

指定したネームスペース配下にルーティングする。

  • Prefix: ○
  • Path: ○
  • Id: ×
  • Controller: ○

コントローラの名前空間とルーティング

namespace :posts do
  resources :comments
end
HTTP Verb Prefix URI Pattern Controller#Action
GET posts_comments /posts/comments posts/comments#index

scope, path

  • Prefix: -
  • Path: ○
  • Id: ×
  • Controller: -
scope :posts do
  resources :comments
end

# 同上
resources :comments, path: '/posts/comments'
HTTP Verb Prefix URI Pattern Controller#Action
GET comments /posts/comments comments#index

module

  • Prefix: -
  • Path: -
  • Id: -
  • Controller: ○
scope module: :posts do
  resources :comments
end

# 同上
resources :comments, module: :posts
HTTP Verb Prefix URI Pattern Controller#Action
GET comments /comments posts/comments#index

as

  • Prefix: ○
  • Path: -
  • Id: -
  • Controller: -

3.6 名前付きルーティング

resources :posts, as: :articles
HTTP Verb Prefix URI Pattern Controller#Action
GET articles /posts posts#index

action

member

指定の action を id 配下に追加する。

メンバールーティングを追加する

resources :posts do
  member do
    get :detail
  end
end

# 同上
resources :posts do
  get :detail, on: :member
end
HTTP Verb Prefix URI Pattern Controller#Action
GET detail_post /posts/:id/detail posts#detail

collection

指定の action を index 配下に追加する。

コレクションルーティングを追加する

resources :posts do
  collection do
    get :search
  end
end

# 同上
resources :posts do
  get :search, on: :collection
end
HTTP Verb Prefix URI Pattern Controller#Action
GET search_posts /posts/search posts#search

on: :new

追加された new アクションへのルーティングを追加する

resources :posts do
  get :preview, on: :new
end
HTTP Verb Prefix URI Pattern Controller#Action
GET preview_new_post /posts/new/preview posts#preview

to, action & controller

Controller と Action を指定する。

単数形リソース

get :profile, to: 'users#show'
# 同上
get :profile, controller: :users, action: :show
HTTP Verb Prefix URI Pattern Controller#Action
GET profile /profile users#show

redirect

リダイレクト

get :posts, to: redirect('/articles')
get '/posts/:id', to: redirect('/articles/%{id}')
HTTP Verb Prefix URI Pattern Controller#Action
GET posts /posts redirect(301, /articles)
GET /posts/:id redirect(301, /articles/%{id})

resource

単数リソースとして扱う。index と :id がなくなる。

単数形リソース

resource :profile
HTTP Verb Prefix URI Pattern Controller#Action
GET new_profile /profile profiles#new
GET edit_profile /profile/edit profiles#edit
GET profile /profile profiles#show
PATCH/PUT /profile profiles#update
DELETE /profile profiles#destroy
POST /profile profiles#create

shallow

ネストを必要最低限にする。

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

# 同上
resources :posts do
  shallow do
    resources :comments
  end
end

# 同上
resources :posts do
  resources :comments, shallow: true
end

# 同上
resources :posts do
  resources :comments, only: [:index, :new, :create]
end
resources :comments, only: [:show, :edit, :update, :destroy]
HTTP Verb Prefix URI Pattern Controller#Action
GET post_comments /posts/:post_id/comments comments#index
POST /posts/:post_id/comments comments#create
GET new_post_comment /posts/:post_id/comments/new comments#new
GET edit_comment /comments/:id/edit comments#edit
GET comment /comments/:id comments#show
PATCH/PUT /comments/:id comments#update
DELETE /comments/:id comments#destroy

Discussion