Skip to content

Instantly share code, notes, and snippets.

@rubiii
Last active April 11, 2018 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubiii/6d8ac2d32f2633464614f80d090aaf21 to your computer and use it in GitHub Desktop.
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
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
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