Wednesday, January 16, 2008

redirect_back_or_default in Rails 2.0

Since they removed redirect_back_or_default in Rails 2.0, and I still had a need to do something like that, I made method you can put under the private portion of your ApplicationController:

def redirect_back_or(path)
redirect_to :back
rescue ActionController::RedirectBackError
redirect_to path
end

and you use it just like the redirect_back_or_default method.

4 comments:

HardinHomes said...

Thanks this was just what I was looking for.

В.Буянмөнх said...

Perfect. it is what I searching for.

Anonymous said...

This link says it was never a part of rails, but lib/login_system.rb instead.

http://www.ruby-forum.com/topic/3081

Anonymous said...

yeeh it's true

and this is the code of that method

def redirect_back_or_default(default)
redirect_to(session[:return_to] || default)
session[:return_to] = nil
end

thanks any way and you code is good also