LoginSignup
0
0

More than 1 year has passed since last update.

Rails ルーティングの設定方法

Last updated at Posted at 2022-02-12

rootの設定

アプリケーションのトップページにあたるrootの設定。

root to: 'home#index'
root     'home#index'
# どちらでも可

個別のルーティングを一つ一つ設定

基本的なリソースを操作するルーティング。

get    'users',          to: 'users#index'  # user一覧を表示するためのアクション
get    'user/:id',       to: 'users#show'   # パラメーターのIDに合致するuserを取得したいときにしようするアクション
get    'users/new',      to: 'users#new'    # user modelに新しいデータを作成するページを表示するアクション
get    'users/:id/edit', to: 'users#edit'   # 作成済みのuserデータを編集する画面を表示するときにしようするアクション
post   'users',          to: 'users#create' # 'users#new'で入力したパラメータを受け取りデータベースに登録するためのアクション
patch  'users/:id',      to: 'users#update' # 'users#edit'で入力したパラメーターを受け取りデータを更新するためのアクション
delete 'users/:id',      to: 'users#destroy'# パラメーターのIDに合致するuserデーターを削除するためのアクション
# get 'users' => 'users#index'のような書き方も可

resources

上記のルーティングを一括で作成できる書き方。

resources :users # userモデルを操作する7つのルーティングが作成される

# 7つのルーティングの中で指定したルーティングのみを作成する書き方
resources :users only:   [:index, :show] # index, show アクションのみを作成する
resources :users only: %i[index, show] # 上と同じ意味
resources :users expect: [:new, :create, :edit, :update, :destroy] #上と同じ意味'expect'は作成しないルーティングを指定する書き方

resource

resourcesを単数系にした書き方。違いはIDを伴うルーティングが作成されないこと。

resource :admin # 単数系、アプリケーションに一人(一つ)しかないリソースのアクションを設定するときに使用

作成されるルーティング一覧

# リソースが一つしか存在しないため'index'アクションは必要ない

get    'admin',       to: 'admins#show' 
get    'admin/new',   to: 'admins#new'
get    'admin/edit',  to: 'admins#edit'                                                                                             
patch  'admin',       to: 'admins#update'                                                                
put    'admin',       to: 'admins#update'                                                                 
delete 'admin',       to: 'admins#destroy'                                                                
post   'admin',       to: 'admins#create'

# idで絞り込む必要もないためURLにIDを含める必要もなくなる

collection

resources、resourceで作成したルーティングに「ID」を伴わない(全体に関係する)ルーティングを新たに追加したいときの書き方。

resources :users do
    collection do
    get :special_offer #ある条件に合致した特定のユーザーを抜き出して処理をしたいとき
  end
end
# users/special_offer と言うURLが作成される

member

‘collection’と似た機能、こちらは「ID」を伴う(個別のリソースに対して)ルーティングを新たに追加したいときの書き方。

resources :users  do
  member do
    get :billing # userに課金をしてもらうページを追加する
  end
end
# users/:id/billing と言うURLが作成される

ネストしたルーティング

「あるuserがたくさんのtaskを持っている」様なモデルの関係性があったとします。そのような場合 に‘users/1/tasks’ といったルーティングを簡潔に記述する書き方。

resources :users do
    resources :tasks
end

作成されるルーティング一覧

get    'users/:user_id/tasks',          to: 'tasks#index' 
get    'users/:user_id/tasks/:id',      to: 'tasks#show'  
get    'users/:user_id/tasks/new',      to: 'tasks#new'   
get    'users/:user_id/tasks/:id/edit', to: 'users#edit'  
post   'users/:user_id/tasks',          to: 'users#create'
patch  'users/:user_id/tasks/:id',      to: 'users#update'
put    'users/:user_id/tasks/:id',      to: 'users#update'
delete 'users/:user_id/tasks/:id',      to: 'users#destroy'

# どちらか一方が'resouce'となっている場合、'user_id','id'の部分を排除したURLを作成することができます。

resources :hoge do ~を何回も重ねて書くことはできますが、管理が難しくなるためネストは1回までが推奨されている様です。

namespace

namespaceで区切ることで、’resouces :users’を指定しても管理者側のルーティンングとユーザー側のルーティングを分けて設定することができる。

# 通常のルーティング
resources :users

# namespaceを利用したルーティング
namespace :admin do
  resources :users
end 

作成されるルーティング一覧

# 通常のルーティング
get    'users',          to: 'users#index'  
get    'users/:id',      to: 'users#show'   
get    'users/new',      to: 'users#new'    
get    'users/:id/edit', to: 'users#edit'   
post   'users',          to: 'users#create' 
patch  'users/:id',      to: 'users#update' 
delete 'users/:id',      to: 'users#destroy'

# namespaceを使用したルーティング
get    'admin/users',          to: 'users#index'  
get    'admin/users/:id',      to: 'users#show'   
get    'admin/users/new',      to: 'users#new'    
get    'admin/users/:id/edit', to: 'users#edit'   
post   'admin/users',          to: 'users#create' 
patch  'admin/users/:id',      to: 'users#update' 

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0