Rails redirect to page user wanted to view prior to login

authentication, ruby-on-rails, ruby-on-rails-3, ruby-on-rails-3.1, session

Solution

I usually use a before_filter like

def login_required
  session[:return_to] = request.fullpath
  redirect_to(login_page_url)
end

Then when my login completes, I look for the return_to in the session:

def login
  # user validation of login here
  url = session[:return_to] || root_path
  session[:return_to] = nil
  url = root_path if url.eql?('/logout')
  logger.debug "URL to redirect to: #{url}"
  redirect_to(url)
end

Problem

I'm trying to redirect users back to a page that they were trying to access prior to logging in (the page is only viewable to users). I'll start off with a before filter that targets my trip new action: `before_filter :authenticate, :only => [:new, :create, :edit, :update, :destroy]` ``` def authenticate deny_access unless signed_in? end def deny_access store_location redirect_to login_path, :notice => "Please log in to access this page." end def store_location session[:return_to] = request.fullpath end ``` Here is the new and create actions in my sessions_controller.rb ``` def new @title = "Log in" end def create user = User.authenticate(params[:session][:email].downcase, params[:session][:password]) if user.nil? flash.now[:error] = "Invalid email/password combination." @title = "Log in" render 'new' else sign_in user redirect_back_or(root_path) end end def redirect_back_or(default) redirect_to(session[:return_to] || default) clear_return_to end ``` Thanks! Edit: Removed MaddHacker's version of my code as that is not what I want.

Original source