Rails serving an alternate public_path

The introduction of the new Rails.public_path setter seems useful only for page caching particulars. If you want to get Mongrel via script/server to serve up static content from an alternate root on request, you actually need to monkey patch your way into the Rack stuff, specifically Rails::Rack::Static.

module Rails
  module Rack
    class Static
      def initialize(app)
        @app = app
        @file_server = ::Rack::File.new(File.join(Rails.root, "..", ENV['DIR']))
      end
    end
  end
end

The above placed in a file in RAILS_ROOT/config/initializers will use an environment variable named DIR to determine the correct path for public assets, like images and static HTML.

I haven’t thoroughly tested the above. It could be it doesn’t pull on an existing Rails.public_path for a reason, or it simply hasn’t been a feature request.

Have fun!

Post a Comment

Your email is never shared. Required fields are marked *

*
*