Devise redirect back to the original location after sign in or sign up?

前端 未结 8 2258
野趣味
野趣味 2020-12-14 07:42

Here I\'m using Devise Gem for authentication. If someone want to open page without login then it redirect to sign_in page and after signed in it back to the page which user

8条回答
  •  轮回少年
    2020-12-14 08:41

    gem 'devise', '~> 4.4.0'

    1. Create sessions_controller.rb: class SessionsController < Devise::SessionsController

    2. Add the following (modifying the regex if your login url isn't /users)

      def before_login
        session[:previous_url] = request.fullpath unless request.fullpath =~ /\/users/
      end
      
      def after_login
        session[:previous_url] || root_path
      end
      

    Notice that this does not work if you've got /users/dashboard or other locations under /users. Might want to get more specific with the regex.

提交回复
热议问题