Monday 26 June 2017

Friendly Forwading in Rails

A user who is not logged in but tries to access a protected page is redirected to login page. After the user logs in, they should be directed to the page they initially requested for (just to make the application user friendly), rather than the default( may be the profile page of user) page. For this to achieve, the requested page can be stored in the session:

session[:forwarding_url] = request.original_url if request.get?
and then the user is redirected to the login page.

After successful login, the user should be directed to the requested page, if
session[:forwarding_url] is present, else should be directed to the 
default page:
 
redirect_to (session[:forwarding_url] || default)
session.delete(:forwarding_url)
Do not forget to delete the session key.