LoginSignup
0
0

More than 1 year has passed since last update.

authenticate_user!メソッドの使い方

Last updated at Posted at 2022-05-15

どんなメソッドか?

ログイン状態によって表示するページを切り替えるメソッドです。
ログインしていないユーザーを、ログイン画面に遷移したい場合に使います。
before actionで呼び出すことにより、コントローラーのアクションを実行する前に、ユーザーがログインしていなければログイン画面に遷移することができます。

before action authenticate user!

onlyの後にアクションを指定した場合の例

before action authenticate user!, only: [:new, :edit]

上記の場合、ログインしていないユーザーがnewアクションとeditアクションを実行した場合のみ、ログイン画面に遷移することができます。
only以外にもexpectの後にアクションを指定することで、ログインしていないユーザーが指定したアクション以外を実行した場合に、ログイン画面へ遷移させることもできます。

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