I recently created something that actually does this. However there *is*
a lot of overhead (another ActionController instance has to start). If
you include the ActionDelegate module (code below) in a controller you
can do:
class MyController < ApplicationController
delegate :new, :to => :controller_name
end
The basics of for this code, I got from he Components code by the way.
Kind Regards,
Flurin
---
module ActionDelegate
def self.append_features(base)
super
base.extend(KlassMethods)
base.send(:include,ActionDelegate::InstanceMethods)
end
module KlassMethods
def delegate(action,options)
module_eval <<-"end;"
def #{action}
nparams = @params.dup
nparams[:action] = ''#{action}''
nparams[:controller] = ''#{options[:to]}''
delegated_controller(nparams)
end
end;
end
end
module InstanceMethods
def delegate(controller_class,params)
render_text(controller_class.process(create_request(params),@response).body,response.headers["Status"])
end
def delegated_controller(params)
delegate(the_class(params),params)
end
def the_class(options)
options[:controller] ? (options[:controller].camelize +
"Controller").constantize : self.class
end
def create_request(options)
request_for_component = @request.dup
request_for_component.send(
:instance_variable_set, :@parameters,
(options[:params] || {}).merge({ "controller" =>
options[:controller],
"action" => options[:action], "id" => options[:id]
}).with_indifferent_access
)
return request_for_component
end
end
end
Josef Pospíšil wrote:
> And what about component.
> http://rails.rubyonrails.org/classes/ActionController/Components.html
>
> If I got it right :-)
> --
> Pepe
> On 17.4.2005, at 19:56, Steve Sloan wrote:
>
>> Is there anyway to invoke an action on another controller? I keep
>> running into cases where it would be nice to render actions from
>> multiple controllers on the same page (i.e. for a summary page, or
>> for GeekLog-style blocks). Preferably, this would be a call similar
>> to render_action, e.g. "render_action :controller =>
''foo'', :action
>> => ''recent''".
>>
>> Obviously, this would require instantiating a new controller
>> instance, initialized with request variables (i.e. @request,
>> @session, @params, et al.). I don''t suppose there''s
something
>> equivalent to the C++ copy constructor, to instantiate each other
>> controller with the instance variables from the current one? Just a
>> thought.
>>
>> Thanks ...
>>
>> -- Steve
>> _______________________________________________
>> Rails mailing list
>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails