Forum: Ruby on Rails Executing multiple controllers with one request

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Chris S. (Guest)
on 2009-05-03 09:05
(Received via mailing list)
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

<code>
Rails::Initializer.run do |config|
    config.middleware.use MyRouter "rpc"
end
</code>

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

<code>
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
</code>

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?
Bagus O. (Guest)
on 2015-02-16 23:54
Ever get anywhere with this approach to dispatch actions to multiple
controllers during a single request?
This topic is locked and can not be replied to.