1
1

More than 3 years have passed since last update.

[Ruby] 詳細ページなどに直接URLを入力されても他のページに遷移させる方法

Posted at

初めての投稿なのでアドバイスあればよろしくお願いいたします!

フリマアプリ作成中、詳細ページまで作成してふと、
「これって直接詳細ページ飛べちゃうやん?」
と思い調べて自分なりにまとめてみました。

items_controller.rb
class ItemsController < ApplicationController

  before_action :set_item, only: [:show, :edit]

   def edit
#重要なのはここから@itemは、before_actionからきてます
    if @item.user == current_user
#current_userは、ログインしているユーザーのこと
#@item.userは、@itemに入ってるuserを出してます
      render "edit"
    else
#もし等しくなければ、redirect_toでroot_path(ホーム画面に戻っています)
      redirect_to root_path
    end

private

  def set_item
    @item = Item.find(params[:id])
  end

  def item_params
    params.require(:item)
          .permit(:image, :name, :explanation, :category_id, :item_status_id,
                  :shipping_fee_status_id, :area_id, :scheduled_delivery_id, :price).merge(user_id: current_user.id)
  end
end
1
1
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
1
1