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
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.
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)