1
0

More than 3 years have passed since last update.

deviseでログインしていない場合はログイン画面に遷移するメモ

Posted at

ログインしていない場合はログイン画面に遷移する

authenticate_user!メソッド

authenticate_user!メソッドは、deviseのヘルパーメソッドである。authenticate_user!メソッドを使用すると、処理が呼ばれた段階で、ユーザーがログインしていなければ、そのユーザーをログイン画面に遷移させる。つまり、ログイン状態によって表示するページを切り替えることができる。

before_actionで呼び出すことで、アクションを実行する前にログインしていなければログイン画面に遷移させることができる。



(記述例)

コントローラーの先頭に、before_action :authenticate_user!と記述

app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  before_action :authenticate_user!
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