Redirect to where I was

I wonder if there is a standard Rails way to redirect from an action
back to a previously viewed page. As a quick fix I created two little
methods in the controllers/application.rb. Does this seem like an ok
way to go?

Thanks,
Peter

class ApplicationController < ActionController::Base

def i_was_here
session[:i_was_here] = request.env[‘REQUEST_URI’]
end

def redirect_to_where_i_was
redirect_to session[:i_was_here]
end

end

Peter M. wrote:

I wonder if there is a standard Rails way to redirect from an action
back to a previously viewed page. As a quick fix I created two little
methods in the controllers/application.rb. Does this seem like an ok
way to go?

Thanks,
Peter

class ApplicationController < ActionController::Base

def i_was_here
session[:i_was_here] = request.env[‘REQUEST_URI’]
end

def redirect_to_where_i_was
redirect_to session[:i_was_here]
end

end

One of the login systems used a similar approach. You could even add a
before_filter on certain actions to run the ‘i_was_here’ method
automagically. You do run the danger of having a
redirect_to_where_i_was go back to itself, thus creating an infinite
loop. You might want to do a little checking to make sure the current
location and the target location aren’t the same.

There’s also:

redirect_to :back

but bear in mind that it will only work if you got to the current page
by actually clicking a link (i.e. where your browser gave some referer
information)

  • james

Why use session?

class ApplicationController < ActionController::Base
def i_was_here
request.env[‘REQUEST_URI’]
end

 def redirect_to_where_i_was
   redirect_to i_was_here
 end

end

But…what’s this about???

redirect_to :back


– Tom M.

Tom M. wrote:

Why use session?

class ApplicationController < ActionController::Base
def i_was_here
request.env[‘REQUEST_URI’]
end

 def redirect_to_where_i_was
   redirect_to i_was_here
 end

end

But…what’s this about???

redirect_to :back


– Tom M.

If you use a session, the place you go back to can be 3 or 4 pages
earlier and/or set programmatically.