Forum: Ruby on Rails Best Way To Get Where You Have Been

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9904f8acaf66a3c1a4b4434676be11cf?d=identicon&s=25 john (Guest)
on 2005-11-13 23:17
(Received via mailing list)
Maybe not the most descriptive subject! :-)

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 Kopanas

--
http://www.soen.info - where software engineering knowledge gets indexed
http://cusec.soen.info - software engineering conference
C618e1d9ffa1bd1a6b6edc8ad4edec63?d=identicon&s=25 mannl (Guest)
on 2005-11-13 23:53
(Received via mailing list)
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
C618e1d9ffa1bd1a6b6edc8ad4edec63?d=identicon&s=25 mannl (Guest)
on 2005-11-14 00:08
(Received via mailing list)
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
Fb7947fdf96a2811ea79d7aef3cbf260?d=identicon&s=25 ryan (Guest)
on 2005-11-14 00:20
(Received via mailing list)
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 Kopanas" <john@protoseinc.com>
To: <rails@lists.rubyonrails.org>
Sent: Sunday, November 13, 2005 5:14 PM
Subject: [Rails] Best Way To Get Where You Have Been
05d703f649ef1d07e78d7b479fb4c4ac?d=identicon&s=25 james.adam (Guest)
on 2005-11-14 01:20
(Received via mailing list)
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
5565e460a42fbe5669b458b2753f8d34?d=identicon&s=25 codeslush (Guest)
on 2005-11-14 01:41
(Received via mailing list)
Here is a link to a blog that helped me solve a similar problem:

http://www.bigbold.com/snippets/tag/rails/2

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


ryan@ryanripley.com 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 Kopanas"
To:
Sent: Sunday, November 13, 2005 5:14 PM
Subject: [Rails] Best Way To Get Where You Have Been
24d2f8804e6bb4b7ea6bd11e0a586470?d=identicon&s=25 jeremy (Guest)
on 2005-11-14 01:44
(Received via mailing list)
-----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-----
This topic is locked and can not be replied to.