はじめに
resourcesメソッドについて、備忘録として残しておきます。
resourcesメソッド
・resourcesメソッドはroutes.rbファイルの中に書き込むメソッドです。
・Railsの基本となる7つのアクションのルーティングをまとめて追加することができます。
Railsの基本となる7つのアクション
基本となる7つのアクションを以下に記載します。
| アクション | HTTP | 役割 | URL |
|---|---|---|---|
| index | get | リソースの一覧を表示する。 | /users |
| show | get | リソースの詳細を表示する。 | /users/:id |
| new | get | リソースを新規作成する。 | /users/new |
| create | post | リソースを新規作成して、保存する。 | /users |
| edit | get | リソースを編集する。 | /users/:id/edit |
| update | put/patch | リソースを更新させる。 | /users/:id |
| destroy | delete | リソースを削除する。 | /users/:id |
定義の仕方
まずは、resourcesメソッドを定義していない状態でルーティングを確認し、何も定義されていないことを確認します。
続いて、以下のようにresourcesメソッドを定義していきます。
※「users」の部分には作成したコントローラ名が入ります。今回はusers_controller.rbを例に行います。
Rails.application.routes.draw do
# resourcesメソッド定義する
resources :users
end
定義した後にルーティングを確認すると・・・
基本的な7つのアクションのルーティングが追加されていることがわかります。
Prefix Verb URI Pattern Controller#Action
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
resourcesメソッドのオプション
ここからは、resourcesメソッドのオプションをめもしておきます。
only
7つのアクションのうち、特定のアクションのみを指定したい時に使用できます。
Rails.application.routes.draw do
# createアクションとnewアクションのみ
resources :users, only:[:create, :new]
end
ルーティングを見てみると、こんな感じになっています。
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
memeber
memberメソッドは、7つのアクション以外のアクションを追加することができます。
まずは、定義方法について、以下記載します。
Rails.application.routes.draw do
# memberメソッドの定義方法
resources :users do
member do
get :following, :followers
end
end
このように定義することで、7つのアクション以外に以下のようなルーティングが得られます。
memberメソッドでは、idで指定した個々のリソースに対するアクションを定義できます。
Prefix Verb URI Pattern Controller#Action
following_user GET /users/:id/following(.:format) users#following
followers_user GET /users/:id/followers(.:format) users#followers
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
collection
collectionメソッドもmemberメソッドと同様に7つのアクション以外のアクションを追加することができます。
memberメソッドと違う点は、collectionメソッドはリソース全体に対するアクションを定義するという点です。
定義方法もmemberメソッドと同様です。
Rails.application.routes.draw do
# collectionメソッドの定義方法
resources :users do
collection do
get :following, :followers
end
end
ルーティングを見てみると、memberメソッド同様7つのアクション以外に追加されていることがわかります。
が、URI Patternの箇所が異なります。
collectionメソッドの方は、全てのリソースに対してアクションを定義しているので**:/id**の部分が省略されています。
Prefix Verb URI Pattern Controller#Action
following_user GET /users/following(.:format) users#following
followers_user GET /users/followers(.:format) users#followers
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
【2つのメソッドの比較】
Prefix Verb URI Pattern Controller#Action
## memberメソッド
following_user GET /users/:id/following(.:format) users#following
followers_user GET /users/:id/followers(.:format) users#followers
## collectionメソッド
following_user GET /users/following(.:format) users#following
followers_user GET /users/followers(.:format) users#followers
参考文献
Rails tutorial 第7章 ユーザー登録
https://railstutorial.jp/chapters/sign_up?version=4.2
Rails tutorial 第14章 ユーザーをフォローする
https://railstutorial.jp/chapters/following_users?version=6.0#cha-following_users
【Rails】resourcesメソッドを使ってルーティングを定義しよう!
https://pikawaka.com/rails/resources