Rails: Redirect after sign in with Devise

后端 未结 6 2017
说谎
说谎 2020-12-14 00:32

Is it possible to redirect users to different pages (based on role) after signing in with Devise? It only seems to redirect to the root :to => ... page defined in routes.rb

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-14 00:56

    https://github.com/heartcombo/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in,-sign-up,-or-sign-out

    I used example 1:

    class ApplicationController < ActionController::Base
      protect_from_forgery with: :exception
    
      before_action :authenticate_user!
    
      protected
    
      def after_sign_in_path_for(resource)
        current_user.is_a?(Admin) ? admin_tests_path : (stored_location_for(resource) || root_path)
      end
    end
    

提交回复
热议问题