Last active
April 11, 2018 17:26
-
-
Save rubiii/6d8ac2d32f2633464614f80d090aaf21 to your computer and use it in GitHub Desktop.
Rails: Redirect users to the last page they were trying to access after login
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
before_action :set_return_to | |
def set_return_to | |
if !request.xhr? && request.format.html? && request.get? | |
session[:return_to] = request.url | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SessionsController < ApplicationController | |
skip_before_action :set_return_to | |
def create | |
user = User.authenticate(params) | |
if user | |
set_current_user(user) | |
redirect_to session[:return_to] || root_path | |
else | |
redirect_to login_path | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment