Let’s say I have an image that does not reside in the normal location:
{appname}/public/images/unconventional.gif
But instead here:
{appname}/unconventional.gif
I understand this is a complete violation of Rails conventions, is immoral and you should never do this under any circumstances and, furthermore, why would I even suggest such a foolish thing?
Ok, now that we have that out of the way, assuming I am on Windows and therefore symbolic links are out of the question, how is it possible to set this up?
Rails does not serve these images, it lets the web server do that. You had best change the configuration of your web server to handle this scenario. If you use
Apache, for example, it would fairly easy to set up withmod_rewrite.Making Rails serve these images will be ugly, but it is possible if you provide a route in your
routes.rbthat matches/public/images/unconventional.gif, and if the file itself does not exist. For example:And then create a controller
StaticImageController:Warning: if you use the above concept, note that if you use input from the URL to decide which file to serve (with
params[:file], for example), you need to thoroughly sanitize the input, because you are risking exposing your entire file system to the outside world.