Executing multiple controllers with one request


#1

I’m designing an Ajax RPC Router for Rails using Ext JS library.

I need to dispatch actions to multiple controllers during a single
request.

I made a piece of Rack Middleware and plugged it in to Rails
initializer in environment.rb

Rails::Initializer.run do |config| config.middleware.use MyRouter "rpc" end

The following Rack Middleware is far from being complete. I show
dispatching to hard-coded controller/actions here as an example.

class MyRouter def initialize(app, rpc_path) @app = app @rpc_path = rpc_path end

def call(env)
request_env = env.dup // <-- have to duplicate env for each
request
if request_env[“PATH_INFO”].match("^"+@rpc_path) # <-- detect rpc
path
output = [] # <-- capture each controller’s response into an
array
[“company”, “user”].each do |controller_name|
request_env[“PATH_INFO”] = “/#{controller_name}/load” # <–
re-write env
request_env[“REQUEST_URI”] = “/#{controller_name}/load”
output << @app.call(request_env) # <-- Is it kosher to call
multiple controllers like this??
end
[200, {“Content-Type” => “text/html”}, process_response(output)]
else
@app.call(env)
end
end

def process_response(list)
# iterate array of controller responses and return the body from
each.
end
end

Is it OK to dispatch to multiple controllers like this?
In Merb, I implemented this RPC functionality using merb-parts.
Is there a similar/better way in Rails?


#2

Ever get anywhere with this approach to dispatch actions to multiple
controllers during a single request?