Rails: Overriding redirect_to.
Coding in a semi-productive site (it's on staging), the client wanted to redirect_to some strange places after some actions.
Most of this redirects defeated the original purpose of the controller's action, so I decided to implement the "return_to on params" pattern to override the redirect at will.
class ApplicationController < ActionController::Base
  def redirect_to(*args)
    if (new_path = params[:return_to]).present?
      logger.debug %Q|return_to found in params. Redirecting to "#{new_path}".|
      super new_path, *args[1..-1]
    else
      super *args
    end
  end
end
I can't decide if it is genius or hacky.
Written by Nicolas Oga
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
 #Rails 
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#