LoginSignup
2
1

More than 1 year has passed since last update.

deviseのログイン後、ログアウト後のリダイレクト先を設定する方法

Posted at

環境

Ruby 3.0.2
Rails 7.0.2.3

コード

app/controllers/application_controller.rb
class ApplicationController < ActionController::Base

  private

  # ログイン後のリダイレクト先
  def after_sign_in_path_for(resource_or_scope)
    root_path  #ここを好きなパスに変更
  end

  # ログアウト後のリダイレクト先
  def after_sign_out_path_for(resource_or_scope)
    new_user_session_path #ここを好きなパスに変更
  end
end

これだけです。

余談

ログアウト後の遷移先は設定必要だと思いますが、
ログイン後の遷移先は、例のように root_pathにするならば、そもそも config/routes.rbで設定してあるので不要だと思います。

config/routes.rb
Rails.application.routes.draw do
  devise_for :users
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

  # Defines the root path route ("/")
  root 'users#index'
end

参考

2
1
3

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