Is it possible to modify the HEADER in Rails 3 when someone requests an image? For example:
http://localhost:3000/assets/myimg.png
I want to add Access-Control-Allow-Origin: “*” to the headers so I can download image files remotely via JavaScript.
UPDATE 1
I have the answer for images under the assets folder to change the headers, but now I also need to do the same thing for public images. I tried the same method below, but no luck.
Example url would be
http://localhost:3000/images/stuff.png
I tried the Rack MiddleWare, but it doesn’t run the call method when going to a public image. I also tried LocationMatch (on a production environment), same no luck.
What do I try for the public images end to change Headers?
UPDATE 2
So, I did manage to get it working via LocationMatch, but the images can not cache. Is there a way to disable image caching via apache?
Assets will be “compiled” and served directly by the web server (Nginx/Apache) so Rails will not serve it in production. You will need to modify the configuration of your web server to add headers.
Something like this in Apache.
And this in Nginx.
See this guide for further details on precompiling assets.
Update: If you need to apply headers in the development environment you can do so with some custom rack middleware. See this gist for an example.