はじめに
フリマアプリ作成において、出品した商品を削除する機能を実装しました。 単に削除できるだけでなく、「出品者本人しか削除できない」というセキュリティ面や、Railsらしい「DRY(共通化)」なコードの書き方を意識して実装しました。
実装内容
- ルーティングの設定
削除機能には destroy アクションを使用します。
# config/routes.rb
resources :items, only: [:index, :new, :create, :show, :edit, :update, :destroy]
- コントローラーの実装(リファクタリングの活用)
今回、最も意識したのが before_action の活用です。 詳細表示(show)や編集(edit)ですでに使っていた「商品を探す処理(set_item)」を削除機能にも使い回すことで、アクション内をスッキリさせました。
# app/controllers/items_controller.rb
class ItemsController < ApplicationController
before_action :authenticate_user!, only: [:new, :create, :edit, :update, :destroy]
before_action :set_item, only: [:show, :edit, :update, :destroy]
before_action :move_to_index, only: [:edit, :update, :destroy]
def destroy
# before_actionで@itemが準備されているので、これだけでOK!
@item.destroy
redirect_to root_path
end
private
def set_item
@item = Item.find(params[:id])
end
def move_to_index
# ガード句を使って出品者本人かチェック
return if current_user.id == @item.user_id
redirect_to root_path
end
end
- ビューの実装(削除リンク)
link_to を使い、HTTPメソッドに delete を指定します。
<%# app/views/items/show.html.erb %>
<% if user_signed_in? && current_user.id == @item.user_id %>
<%= link_to "削除", item_path(@item.id), data: { turbo_method: :delete }, class:"item-destroy" %>
<% end %>
学んだポイント・苦労した点
-
「定義」と「実行」の切り分け
当初、show アクション内でも @item = Item.find(params[:id]) を記述していましたが、メンターさんのアドバイスで before_action に集約しました。 「実際に作業をするコード(定義)」は1箇所にまとめ、それを「指示(before_action)」で呼び出すという DRY原則(Don't Repeat Yourself) の重要性を学びました。 -
サーバーサイドでの権限管理
ビュー側で削除ボタンを隠すだけでなく、コントローラー側(move_to_index)でも本人確認を行うことで、URLの直接入力による不正な削除を防ぐという、実戦的なセキュリティの考え方を身につけることができました。