How do I serve a file statically with the correct content-type headers if it is not (for good reason) in the "public/" directory?
A:
See Rack::Static.
module MyApp
class Application < Rails::Application
config.middleware.use Rack::Static,
:urls => [ '/my-secret-dir' ],
:root => 'my/secret/dir'
end
end
Justice
2010-08-09 19:39:42
What if I want to serve it dynamically?
Christoffer
2010-08-09 19:40:53
Then use `send_file` as Gareth indicates.
Justice
2010-08-09 19:58:46
Keep in mind that serving a static file by firing up a rails request works, but uses a lot of overhead.
fullware
2010-08-09 20:09:05
Yes, this obviously runs everything through the Rails stack, so only do this if necessary - for example you need to base the download on dynamic authentication.
Gareth
2010-08-09 20:12:52
Yes, I am aware of the overhead. But I cannot think of a better way to solve my problem. I'm building a small CMS with a file based template system, and each template can have it's own public directory, so which public directory to use is based on the template settings. I need the normal public-directory for the administration system.
Christoffer
2010-08-10 06:11:29
Use XSendFile to avoid loading the Rails stack with the file transfers.
Vijay Dev
2010-08-11 05:42:33
You can set up controller action which will catch routes into `/public /template/:id/:filename'. This action would only get called if the file doesn't exist as specified by the path, and it can then generate the file and place it there. Subsequent accesses will hit the asset directly. I personally use this for generating image thumbnails dynamically.
fullware
2010-08-23 16:59:53