Here I\'m using Devise Gem for authentication. If someone want to open page without login then it redirect to sign_in page and after signed in it back to the page which user
You don't need all this code. Devise (as the latest versions) already saves the location for you.
Just use this:
def after_sign_in_path_for(resource)
request.env['omniauth.origin'] || stored_location_for(resource) || root_url
end
This will redirect user to the latest omniauth.source, or the stored_location and is last case, the root url.
I thought I was required to create that method, but Devise already does it.
Source: https://github.com/plataformatec/devise/wiki/How-To:-redirect-to-a-specific-page-on-successful-sign-in