0
0

More than 1 year has passed since last update.

AbstractController::DoubleRenderError

Posted at

はじめに

フリマアプリを実装中のエラーです。
ログイン状態、ログアウト状態で遷移できるページを変更したい実装中に
AbstractController::DoubleRenderErrorのデバックを記載しています。
URLは直打ちで入力する事とします!

実装したいこと

  • ログイン状態でも自身が出品していない編集ページへ遷移しようとするとトップページに遷移する。
  • ログアウト状態で商品編集ページに遷移しようとするとログインページに遷移する。

以上の2点を実装しようとしています。

エラーまでの経緯 ~コード紹介~

ruby items_controller.rb
def edit
    redirect_to new_user_session_path unless user_signed_in?
    if current_user.id != @item.user_id
      redirect_to root_path
    end
end

以上の実装で

AbstractController::DoubleRenderError

エラーが発生しました。

解決

エラー文よりrenderを2回している事が原因だと考え実装したい事を
同じアクション(edit)内で行わない方が良いと考えました!!

  • ログイン状態でも自身が出品していない編集ページへ遷移しようとするとトップページに遷移する。

                 ↓

ruby items_controller.rb
def edit
    redirect_to root_path unless current_user.id == @item.user_id
end
  • ログアウト状態で商品編集ページに遷移しようとするとログインページに遷移する。

                 ↓

ruby items_controller.rb
before_action :authenticate_user!, only: [:new, :edit]

それぞれの条件分岐を分ける事で解決しました!!

まとめ

初心者でなければ起きないエラーだと思いますが
before_action :authenticate_user!を完全に忘れてました、、、泣

ぜひ参考になる方が居れば良いなと思います。
最後までありがとうございました!!

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