Created
June 16, 2010 13:47
-
-
Save andyjeffries/440708 to your computer and use it in GitHub Desktop.
Internal redirect_to for Rails 2.x
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is an internal_redirect_to function by Sandro Paganotti | |
# http://www.railsonwave.com/2008/10/25/how-to-call-a-controller-s-action-from-a-different-controller | |
# By using this internal_redirect_to function you will be able to | |
# get the same results as a normal redirect_to without send anything | |
# to the user, only keep in mind to explicit invoke the return after | |
# this function. | |
class ApplicationController | |
def internal_redirect_to (options={}) | |
params.merge!(options) | |
(c = ActionController.const_get(Inflector.classify("#{params[:controller]}_controller")).new).process(request,response) | |
c.instance_variables.each{|v| self.instance_variable_set(v,c.instance_variable_get(v))} | |
end | |
end | |
# Then you can simply invoke this function by typing: | |
MasterController < ApplicationController | |
def index | |
internal_redirect_to :controller=>'another', :action=>'an_action' | |
return | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment