Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to original destination after logging in

Rails 2.3.11

If a user tries to go to /photos when they aren't logged in, they are directed to [site]/admin/login?route=[site]/photos. After they log in, I would like them to be sent to whatever was defined in "route" instead of the default home page.

In /app/controllers/admin_controller.rb:

def login
    if session[:user_id] #already logged in
      redirect_to ''
    @destination = request.url
    end
    if request.post?
      if [authentication code]
        if user.activated? #check to see whether the user has activated their account
          session[:user_id] = user.id
          if params[:route] # ********
            redirect_to "#{params[:route]}"
          else
            redirect_to :controller => 'home'
          end
        else
          flash.now[:notice] = "Your account hasn't been activated yet.  Check your emails!"
        end
      else
        flash.now[:notice] = "Invalid email/password combination"
      end
    end
  end

The "*"ed line is the one that isn't working correctly. When I check to see the parameters, :route isn't among them, so the argument isn't being passed in with the login post. Can anyone explain to me why it isn't and how I could fix it?

Thank you!

like image 523
Benjin Avatar asked Jul 22 '11 20:07

Benjin


1 Answers

You could do something like:

session[:return_to] ||= request.referer
like image 86
Swift Avatar answered Nov 15 '22 03:11

Swift