1
0

More than 1 year has passed since last update.

AとBが同じではない時について(if 文)

Last updated at Posted at 2022-02-26

簡易的なフリマアプリを作成中。

全てのコードを書き終えて、最終挙動確認を実施。

「自分の出品商品以外の商品には編集ページへ遷移しない」という挙動を確かめるため、直接URLに/editを入力。
すると、意図と反して編集ページに移ってしまう。

該当するソースコードを確認。

controllers/item_controller.rb
private 
def move_to_index 
    if !user_signed_in? && current_user.id == @item.user.id
      redirect_to root_path
    elsif @item.order.present?
      redirect_to root_path
    end
  end

よく見てみると!の位置がおかしい。
これだと「ユーザーがログインしていない時、かつログイン中のユーザーIDと出品した商品のユーザーIDが同じ時」というめちゃくちゃな日本語に笑
※「ログインいしていない時、かつログイン中」という矛盾が生じている

だからPCは「ログインしていない時」という条件をすっ飛ばして、編集ページに遷移させたのか・・・

とういうことで以下のコードに修正。

controllers/item_controller.rb
private 
def move_to_index 
    if user_signed_in? && current_user.id != @item.user.id   #修正箇所
      redirect_to root_path
    elsif @item.order.present?
      redirect_to root_path
    end
  end

これで無事にトップページへ遷移してくれました。

1
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
1
0