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 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 > > endOne 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. -- Posted via http://www.ruby-forum.com/.
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 On 1/8/06, Kevin Olbrich <kevin.olbrich@duke.edu> wrote:> 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. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
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 On Jan 8, 2006, at 10:05 AM, 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
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 MorniniIf you use a session, the place you go back to can be 3 or 4 pages earlier and/or set programmatically. -- Posted via http://www.ruby-forum.com/.