Re: Best Way To Get Where You Have Been


#1

Forgot to add this for the application controller:

Store the current URL so we can redirect back to it if necessary

before_filter :store_locations

Michael removed_email_address@domain.invalid wrote:
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

removed_email_address@domain.invalid 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