Last Updated: February 25, 2016
·
717
· nicooga

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.