I am using a custom Rack middleware in my Rails 3.1 app that wraps around a vanilla Rails controller:
in routes.rb
stacked_router =
Example::Middleware::StackedRouter.new(ProductsController.action(:show))
match '/:id', :to => stacked_router
in example/middleware/stacked_router.rb
class Example::Middleware::StackedRouter
def initialize(app)
@app = app
end
def call(env)
# ... do stuff before forwarding the request, then
@app.call(env) # forward the request
end
end
This works fine.
However there is a catch: When I now change code in ProductsController
, the changes are not picked up automatically. I have to restart the app manually (touch tmp/restart.txt
)
What's the way to tell the Rails stack that it needs to reload this piece of middleware whenever code is changed?
It looks like Rails is loading the copy of ProductsController at the moment the server is started into your middleware, and keeping that cached copy. I am not 100% sure about this, but what happens when you try to load the middleware with a proc? e.g.
That should hopefully make it load a new middleware (and a new ProductsController) on each request. It probably will be unhelpful in production. I don't have similar code in front of me, so I can't test this.