1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

railsでログインしてないユーザーのアクセスを制限したい!

Posted at

deviceというログイン機能の実装を簡単にしてくれるGemの導入前提

1.ログインしてるかどうか確かめる
  user_signed_in? メソッドを使う
  true → ログインしています!
  false → ログインしてないです!

2.リダイレクトする

  before_action :move_to_index, except: [:index, :show]
  def move_to_index
    unless user_signed_in?
      redirect_to action: :index
    end
  end

まずルーティングの振り分けでコントローラーに処理がきた途端にmove_to_indexが実行される
そこでfalseが返り値として確認された途端、ルーティングの振り分けなんかお構いなしでindexへ飛ばされる

1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?