Forum: Ruby on Rails Redirect to where I was

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.
Eea3feaacbe44706164289d068d94828?d=identicon&s=25 Peter Michaux (Guest)
on 2006-01-08 19:07
(Received via mailing list)
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
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-08 19:52
Peter Michaux 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.
05d703f649ef1d07e78d7b479fb4c4ac?d=identicon&s=25 James Adam (Guest)
on 2006-01-08 21:56
(Received via mailing list)
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
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-01-09 00:15
(Received via mailing list)
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 Mornini
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-09 03:12
Tom Mornini 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 Mornini

If you use a session, the place you go back to can be 3 or 4 pages
earlier and/or set programmatically.
This topic is locked and can not be replied to.