views:

142

answers:

1

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?

+5  A: 

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 with mod_rewrite.

Making Rails serve these images will be ugly, but it is possible if you provide a route in your routes.rb that matches /public/images/unconventional.gif, and if the file itself does not exist. For example:

map.connect "public/images/unconventional.gif",
  :controller => "static_image_controller",
  :action => "serve"

And then create a controller StaticImageController:

class StaticImageController < ApplicationController
  def serve
    image = File.read(File.join(Rails.root, "unconventional.gif"))
    send_data image, :type => "image/gif", :disposition => "inline"
  end
end

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.

molf