For the application I am writing, I use simple manual authentication (as described on Railscast.com). Using some code from Rifty Bates' NiftyGenerators Gem, I have an authentication model that has some useful methods for authentication. This module is included in application_controller.rb.
One of the methods that I want to use is called redirect_to_target_or_default. I know that I need to redirect the user to the page they were on after they have authenticated, but I don’t know where I should call this method? If someone could give me an idea of how to use this method, I would really appreciate it.
ControllerAuthenticaion Module Code
module ControllerAuthentication
def self.included(controller)
controller.send :helper_method,
:current_admin, :logged_in?,
:redirect_to_target_or_default
end
def current_admin
@current_admin ||= Admin.find(session[:admin_id]) if session[:admin_id]
end
def logged_in?
current_admin
end
def login_required
unless logged_in?
store_target_location
redirect_to login_url,
:alert => "You must first log in before accessing this page."
end
end
def redirect_to_target_or_default(default, *args)
redirect_to(session[:return_to] || default, *args)
session[:return_to] = nil
end
def redirect_to_or_default(target, *args)
redirect_to(target || default, *args)
end
def store_target_location
session[:return_to] = request.url
end
end