christocracy
unread,May 3, 2009, 1:04:41 AM5/3/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Ruby on Rails: Talk
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?