Redirect back to last page?

I have a few pages where a user may do something (add tags, login, etc)
and I would like to redirect them back to the last page they were at
before calling that action. Is there an easy way to do this?

Vince W. wrote:

I have a few pages where a user may do something (add tags, login, etc)
and I would like to redirect them back to the last page they were at
before calling that action. Is there an easy way to do this?

I have used this since last year, but I am thinking they put something
into core that handles this now and I haven’t changed anything. I could
be wrong though…

application.rb:

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

within your controller:

redirect_back("action => 'list'")

If memory serves me correctly, I added the .length > 4 but can’t for the
life of me remember why! (that’s why you are supposed to comment your
code)

Anyway, hope this helps but don’t be surprised if someone gives you
something better.

Regards,

Michael

On Aug 12, 2006, at 6:02, Vince W. wrote:

I have a few pages where a user may do something (add tags, login,
etc)
and I would like to redirect them back to the last page they were at
before calling that action. Is there an easy way to do this?

redirect_to :back

… as specified in the API reference for redirect_to, who’d have
guessed? :wink:


Jakob S. - http://mentalized.net

Do you happen to know why the “Rails Recipes” book gave the following
code to redirect back to the page? (This is in the “authenticating
users” section, and when we are redirected to, for example, /home/login
from /home)

session[:intended_action] = action_name
session[:intended_controller] = controller_name

Then after signing in:

redirect_to :action => session[:intended_action], :controller =>
session[:intended_controller]

Wouldn’t redirect_to :back do the same thing?

Rob G. wrote:

redirect_to :action => session[:intended_action], :controller =>
session[:intended_controller]

Wouldn’t redirect_to :back do the same thing?

In some cases, sure. But consider this usage pattern:

  1. Request a page, store it’s URL in session
  2. Redirect to Login page
  3. Post Login information
  4. Request Forgot password page
  5. Process Forgot password procedure, which might include multiple
    requests
  6. Request Login page
  7. Post Login information, then redirect to the stored URL

In this case, redirect_to :back in step 7 would take the user to the
login page, or in best case one of the steps in the forgot password
procedure. Whereas redirecting to the URL stored in session would take
the user to where he “left” the normal flow.

I believe ‘redirect_to :back’ isn’t as flexible. What if you send a
user to
the login page, but he has to then go and create a new account? I’m
also
not sure how it would handle the situation in that wrong username /
passwords are entered, and the user is sent to a different error page.

If you take the extra steps to store the intended location, you can
always
make sure you get back to their.

“Vince W.” [email protected] writes:

I have a few pages where a user may do something (add tags, login, etc)
and I would like to redirect them back to the last page they were at
before calling that action. Is there an easy way to do this?

store the url in session variable(session[:back_to]) . Looking at code
of
authentication engines/plugins will give you an idea of how this is
done.

Cheers,

Surendra S.
http://ssinghi.kreeti.com, http://www.kreeti.com
Read my blog at: http://cuttingtheredtape.blogspot.com/
,----
| “All animals are equal, but some animals are more equal than others.”
| – Orwell, Animal Farm, 1945
`----

2006/8/12, Rob G. [email protected]:

session[:intended_action] = action_name
session[:intended_controller] = controller_name

Then after signing in:

redirect_to :action => session[:intended_action], :controller =>
session[:intended_controller]

Wouldn’t redirect_to :back do the same thing?

No. redirect_to :back uses the referer header provided by the browser,
so
its just 1 step back. the session thing you mentioned goes back a
minimum of
2:

  1. request private page
  2. redirect to login
  3. post login
  4. go to 1


Michael S. [email protected]

www.stellar-legends.de - Weltraum-Browsergame im Alpha-Stadium