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
gem 'devise', '~> 4.4.0'
Create sessions_controller.rb: class SessionsController < Devise::SessionsController
Add the following (modifying the regex if your login url isn't /users)
def before_login
session[:previous_url] = request.fullpath unless request.fullpath =~ /\/users/
end
def after_login
session[:previous_url] || root_path
end
Notice that this does not work if you've got /users/dashboard or other locations under /users. Might want to get more specific with the regex.