LoginSignup
0
0

More than 1 year has passed since last update.

[Rails]before_action :authenticate_user!について

Posted at

authenticate_user!とは

・deviseが用意してくれた便利なメソッドである。
・userがログインしているか確認することができる。

具体的な使用法

class ApplicationController < ActionController::Base
  before_action :authenticate_user!

  def index
  end

  def show
  end

  def edit
  end
end

コントローラーの先頭に書くことでログインしていないとコントローラーの内容を実行できない。

私自身、ApplicationControllerに記載してしまったため、何も実行できなかったためこの記事を書きました。

class ApplicationController < ActionController::Base
  before_action :authenticate_user!, only: [show]

  def index
  end

  def show
  end

  def edit
  end
end

上記ように追加するとshoアクションだけログインしないと実行できないようになる
index,editはログインしていなくても実行可能

感想

初心者には、まだまだ覚えることが多そうです。
説明が間違っていればご指摘頂けると幸いです

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