LoginSignup
0
0

More than 3 years have passed since last update.

ログインしていないユーザーをnewやeditページに遷移しない様にする方法

Last updated at Posted at 2020-05-21

今回のやりたい事は

ログインしていないユーザーは、index,showページのみ遷移できて、newページやeditページに遷移しようとすると強制的にindexページに遷移されるようにします。

方法

以下の処理を行うことで、ユーザーがログインしていない状態でindex,showページ以外に遷移しようとすると、強制的にindexページに遷移されるようになります。

controller.rb

class PracticeController < ApplicationController
  before_action :move_to_index, except: [:index, :show]

  ---省略---

  private

  def  move_to_index
    redirect_to action: :index unless user_signed_in?
  end

説明

コントローラー内で繰り返し使用されるコードは、private以下でメソッド化します。

最後まで見て頂きありがとうございます!!

この記事が少しでも参考になれば嬉しいです:pray_tone2:

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