0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Rails初心者向け】resources :usersで理解するRailsのRESTful設計

Last updated at Posted at 2025-10-01

はじめに

Railsでルーティングを設定する際、config/routes.rbにたった一行書くだけで、7つものルートが自動生成されます。

resources :users

この魔法のような一行の背後には、RESTfulという設計思想があります。この記事では、生成される7つのアクションとHTTPメソッドの対応関係を整理して理解していきます。

resources :usersとは

resources :usersは、ユーザーというリソース(Resources)に対する標準的な操作を一括で定義する記法です。

リソースとして扱うということは、データの作成・表示・更新・削除をHTTP標準の4つの基本操作(POST、GET、PATCH、DELETE)に対応させるということです。これがRESTful(Representational State Transfer)な設計の基本です。

生成される7つのルートを確認

実際にターミナルで確認してみましょう。

$ rails routes | grep users

すると、以下のような7つのルートが生成されていることがわかります。

HTTPメソッド URL アクション 役割
GET /users index 全ユーザー一覧
GET /users/:id show 特定ユーザー詳細
GET /users/new new 新規作成フォーム
POST /users create 新規作成処理
GET /users/:id/edit edit 編集フォーム
PATCH/PUT /users/:id update 更新処理
DELETE /users/:id destroy 削除処理

CRUDの基本4操作

まず、特定のユーザーに対する基本的なCRUD操作を見ていきます。

CRUD HTTPメソッド アクション URL例
Create POST create POST /users
Read GET show GET /users/1
Update PATCH/PUT update PATCH /users/1
Delete DELETE destroy DELETE /users/1

これが基本の4つです。すべて「特定のユーザー(:id)」に対するデータベース操作です。

create(POST /users)- Create

新規ユーザーをデータベースに作成します。

# UsersController
def create
  @user = User.new(user_params)
  if @user.save
    redirect_to @user
  else
    render :new
  end
end

パスヘルパー:users_path(POSTメソッド)

show(GET /users/:id)- Read

特定ユーザーの詳細情報をデータベースから取得して表示します。

def show
  @user = User.find(params[:id])
end

パスヘルパー:user_path(@user)/users/1

update(PATCH/PUT /users/:id)- Update

特定ユーザーの情報をデータベースで更新します。

def update
  @user = User.find(params[:id])
  if @user.update(user_params)
    redirect_to @user
  else
    render :edit
  end
end

パスヘルパー:user_path(@user)(PATCH/PUTメソッド)

destroy(DELETE /users/:id)- Delete

特定ユーザーをデータベースから削除します。

def destroy
  @user = User.find(params[:id])
  @user.destroy
  redirect_to users_path
end

パスヘルパー:user_path(@user)(DELETEメソッド)

フォーム表示用の補助アクション

createとupdateは「どんな情報で作成・更新するか」をユーザーに入力してもらう必要があります。そのための入力フォーム表示専用アクションがnewとeditです。

new(GET /users/new)

createの準備として、新規作成用の入力フォームを表示します。

def new
  @user = User.new
end

ユーザーがフォームに入力すると、createアクション(POST /users)へ送信されます。

パスヘルパー:new_user_path/users/new

edit(GET /users/:id/edit)

updateの準備として、更新用の入力フォームを表示します(既存データが入った状態)。

def edit
  @user = User.find(params[:id])
end

ユーザーがフォームを編集すると、updateアクション(PATCH /users/:id)へ送信されます。

パスヘルパー:edit_user_path(@user)/users/1/edit

フォームとデータ操作の流れ

  • new(GET)create(POST):新規作成の流れ
  • edit(GET)update(PATCH):更新の流れ

new/editはフォーム表示だけで、データベースは変更しません。実際のデータベース操作はcreate/updateが行います。

特別枠:index

index(GET /users)

全ユーザーの一覧を表示します。

def index
  @users = User.all
end

パスヘルパー:users_path/users

indexは唯一「特定のユーザー」ではなく「すべてのユーザー」を扱うアクションです。URLに:idがありません。

なぜindexが必要?

管理画面などで「どのユーザーを見る/編集するか」を選ぶために一覧が必要です。ここから個別のshowやeditへ遷移します。

7つのアクションの全体像

[特定ユーザーのCRUD - 基本4操作]
  Create: (new) → create
  Read:   show
  Update: (edit) → update
  Delete: destroy

[全ユーザーの操作 - 特別枠]
  Read:   index

まとめ

  • CRUD基本4つ:create, show, update, destroy(特定ユーザー対象)
  • フォーム用2つ:new, edit(入力の準備)
  • 特別枠1つ:index(全ユーザー一覧)
  • 合計7つのRESTfulアクション

HTTPメソッドとCRUD操作の対応

HTTP操作 意味 対応するアクション
POST 作成(Create) create
GET 取得(Read) index, show, new, edit
PATCH/PUT 更新(Update) update
DELETE 削除(Delete) destroy

GETが4つもあるのは、データ閲覧(index, show)とフォーム表示(new, edit)の両方に使われるためです。すべて「見るだけ」でデータベースは変更しません。

まとめ

resources :usersのたった一行には、HTTP標準の4つの基本操作(POST、GET、PATCH、DELETE)とRESTfulという設計思想が込められています。

  • 基本のCRUD 4操作にフォーム用の補助アクション2つと一覧表示1つを加えた7つ
  • この構造を理解すれば、どんなリソースでも同じパターンで設計できる
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?