For the app that I am writing, I am using simple hand made authentication (as described on Railscast.com). Using some code from Ryan Bates’ NiftyGenerators gem, I have an authentication model that has some useful methods for authentication. This module is included into application_controller.rb.
One of the methods that I want to use is called redirect_to_target_or_default. I know this is what I need to redirect a user to the page that they were on once they have authenticated but I don’t know where I should call this method? If someone could give me an idea on how to use this method, I would greatly appreciate it.
ControllerAuthenticaion Module Code
module ControllerAuthentication
# Makes these methods helper methods in the controller that includes this module.
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
Did you run the generator(s) within Ryan’s gem? It should have generated a SessionController (refer to link) for you with also this method in it:
I think you can get an idea of how to use it while reading this code. 🙂