LoginSignup
0
0

More than 1 year has passed since last update.

deviseの複数モデル使用でauthenticate_user!を使用したい

Posted at

事象

オリジナルアプリを作成していて、アカウアントを分けるためにdeviseの複数モデルを使用している。
authenticate_user!を使用してログインしていない場合はログインページに遷移させる記述を
したかったが、複数モデルを使用しているためどちらかがログインしている場合でも片方の記述が
適用されてしまう。

application_controller.rb
before_action :authenticate_trainer!
before_action :authenticate_cliant!

解決方法

posts_controller.rb
before_action :redirect_root, only: [:edit]

private

def redirect_root
  redirect_to root_path unless cliant_signed_in? or trainer_signed_in?
end

と言う記述に変更する。
ログインページに飛ばすような記述だと、複数モデルのためどちらのログインページに飛ばせばいいか
判断できないため、トップページ飛ばすメソッドをオリジナルで作成することで解決。
ログインしていないユーザーが投稿の編集ページのURLを直接入力すると、ロップページに
遷移する。

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