LoginSignup
2
0

More than 1 year has passed since last update.

【Rails】deviseで複数モデル設定した時の遷移先変更方法【初学者の疑問点を簡潔に解説】

Last updated at Posted at 2021-12-18

はじめに

 本記事は、プログラミングの学習を始めて1ヶ月の初学者が、学習を進めていて疑問に思った点について調べた結果を備忘録も兼ねてまとめたものです。
 そのため、記事の内容に誤りが含まれている可能性があります。ご容赦ください。
 間違いを見つけた方は、お手数ですが、ご指摘いただけますと幸いです。

deviseで複数モデル設定時の遷移先の変更方法

application_controller.rb
class ApplicationController < ActionController::Base

    # ログイン後のリダイレクト先
    def after_sign_in_path_for(resource_or_scope)
      if resource.is_a?(Admin)
        admin_orders_path
      else
        root_path
      end
    end

    # ログアウト後のリダイレクト先
    def after_sign_out_path_for(resource_or_scope)
      if resource_or_scope == :admin
        new_admin_session_path
      else
        root_path
      end
    end
end

または

application_controller.rb
class ApplicationController < ActionController::Base

 def after_sign_up_path_for(resource)
   case resource
   when Admin
     admin_orders_path
   when EndUser
     root_path
   end
 end

 def after_sign_out_path_for(resource)
    case resource
    when :admin   # ログアウト時はシンボルが返ってくる
      new_admin_session_path
    when :end_user  # ログアウト時はシンボルが返ってくる
      root_path
    end
 end

end

おわりに

詳細な説明については、後日追記する予定です。

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