Best Way To Get Where You Have Been

Maybe not the most descriptive subject! :slight_smile:

I have a filter that redirects the user to a login page if they have
not logged in on certain pages. Now what is the best way to get the
page they where initially requesting and then send the user to that
page after they have successfully logged in?

John K.


http://www.soen.info - where software engineering knowledge gets indexed
http://cusec.soen.info - software engineering conference

You can do:

redirect_to :back

From the manual:

:back: Back to the page that issued the request. Useful for forms
that are triggered from multiple places. Short-hand for redirect_to
(request.env[“HTTP_REFERER”])”

Rob

PS: However, that only works if you have the login form embedded in
your pages. If you have an link that takes the user to a intermediary
login form, then the solution I proposed doesn’t work. There’s
probably another way to do it then, but I don’t know about it.

Rob

Save it in the session before you redirect…

session[:where_you_wanted_to_go] = request.parameters

Then redirect to login

redirect_to (:controller => “login”, :action => “login”)

The code below goes in your login controller and executes once login is
successful:

where_you_wanted_to_go = session[:where_you_wanted_to_go] || {:action =>
“default_page”}
session[:where_you_wanted_to_go] = nil
redirect_to(where_you_wanted_to_go)

I may not have this 100%, I’m pulling it from memory, I believe I saw it
covered in the Agile Rails Development book…

Hope this helps,

–Ryan

----- Original Message -----
From: “John K.” [email protected]
To: [email protected]
Sent: Sunday, November 13, 2005 5:14 PM
Subject: [Rails] Best Way To Get Where You Have Been

Most off the login systems spawned from the original login generator
include two methods which try to handle this: store_location and
redirect_back_or_default - you can look at the login generator, salted
hash login generate, login engine and acts_as_authenticated
generator/plugin for examples of these methods to copy, and how to use
them in your own application.

  • james

Here is a link to a blog that helped me solve a similar problem:

I took the code from the site and added two minor changes to handle an
empty session and new/create duplication and it works beautifully for
me. Just add the redirect_back to your actions.

Regards,

Michael

Application Controller:

Store where we are

def store_locations

if @session[‘prevpage’] && @session[‘thispage’] != @request.request_uri

#If this is a “create” action, the do not do anything, because it will
just take us to the new

unless @request.request_uri.index(‘create’)

@session[‘prevpage’] = @session[‘thispage’] || ‘’

@session[‘thispage’] = @request.request_uri

end

else

@session[‘prevpage’] = @request.request_uri

@session[‘thispage’] = @request.request_uri

end

end

redirect_back

If a previous page is stored in the session, go back to it… otherwise

go back to a default page

def redirect_back(default)

if @session[‘prevpage’].nil?

if default

redirect_to default

else

redirect_to :controller => “”, :action => “”

end

else

if @session[‘prevpage’].length > 4

redirect_to_url @session[‘prevpage’]

else

redirect_to default

end

end

end

[email protected] wrote:
Save it in the session before you redirect…

session[:where_you_wanted_to_go] = request.parameters

Then redirect to login

redirect_to (:controller => “login”, :action => “login”)

The code below goes in your login controller and executes once login is
successful:

where_you_wanted_to_go = session[:where_you_wanted_to_go] || {:action =>
“default_page”}
session[:where_you_wanted_to_go] = nil
redirect_to(where_you_wanted_to_go)

I may not have this 100%, I’m pulling it from memory, I believe I saw it
covered in the Agile Rails Development book…

Hope this helps,

–Ryan

----- Original Message -----
From: “John K.”
To:
Sent: Sunday, November 13, 2005 5:14 PM
Subject: [Rails] Best Way To Get Where You Have Been

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Nov 13, 2005, at 2:52 PM, Robert wrote:

You can do:

redirect_to :back

From the manual:

:back: Back to the page that issued the request. Useful for forms
that are triggered from multiple places. Short-hand for redirect_to
(request.env[“HTTP_REFERER”])”

This won’t work:

  • redirect to /login
  • post form to /login
  • redirect :back sends you to the REFERER, which is /login

You can use something like:
def redirect_back(fallback_options)
redirect_to params[:return_to] || session[:return_to] ||
fallback_options
end
and pass :return_to as a hidden form parameter or set session
[:return_to] = request.request_uri before you redirect to login.

jeremy
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFDd93tAQHALep9HFYRAj0oAJ9RRn5NWnCzX78HyfizjsMpbFDEXwCgkzZ9
luDOitdtG2oMjonc1XwH0Jo=
=RAoG
-----END PGP SIGNATURE-----