Q:

Rails: Warden/Devise - How to capture the url before login/failed access

I am trying to figure out how to redirect a user to the page they logged in from (or failed to login) using Warden/Devise.  I figure there is a session variable somewhere that is either available or could be made available. 
E.g.
Scenario 1:
Non-authorized user goes to protected page X;
Redirected to login page;
User logs in;
User redirected to protected page x
Scenario 2:
Non-authorized user wants to take a protected action on page x;
User clicks on login link;
User logs in;
User redirected to page x where the action is now available
Any pointers are appreciated. 
Thanks! 

A:

There is a devise helper method called after_sign_in_path_for(resource) (http://rdoc.info/github/plataformatec/devise/master/Devise/Controllers/Helpers) , and a session variable called session[:"user.return_to"] which stores the last url.  The after_sign_in_path_for method needs to return a string, then devise automatically uses this path to redirect the user after login. 
In my application controller I have put the following which redirects my users to the home page if the session variable is not set:
def after_sign_in_path_for(resource)
    (session[:"user.return_to"].nil?) ? "/" :  session[:"user.return_to"].to_s
end

