Linking to images outside public folder

I have a project where I need to show images (.jpg’s) in the browser
that are outside my projects “public” directory. I’m developing on a
Mac and I’ve tried using an absolute path “/Users/my_name/Desktop/
image.jpg”. I can use this technique to read the contents of .txt
files and send it as a string to the browser but I can’t get images to
display. Do I need to use send_file or send_data to get the images to
display? Any help would be great. Thanks.

Hi Urban,

i am relatively new to Rails, so i may be wrong, but i had the same
recently and resorted to putting smaller images in the public directory,
which are displayed. The original (larger) images cannot be displayed in
app, but they can be downloaded. For downloading i used send_file.
If i am right in my assumption that there is no way to use image_tag
files outside of the app’s directory structure and it is vital for your
to do so, i would try to put the images outside and copy them to a
beneath the public directory whenever you have to display them (and
them afterwards). Surely, that would not improve performance…
Another thing i read of to solve the problem of having files in a public
directory, but not wanting people to access them freely: Put them in
directories and make those directories’ names unguessable - this way you
will allow people to access single images, but they cannot access them
And last (but i read not much good about the method): You could put the
images as blobs in the db.


2007/6/11, urbanf <[email protected] >:

if your using the image_tag helper I thin that automatically points to
your public directory. Try just using an old skool



The browser can only request things by a URI and that will be served
by the web listener if it’s a resource (file) under the document
root. Typically, this is the public/ directory. Other resources
might be available to a request being serviced by your Rails
application. You could return the image data as a result of the
request using either of the methods you mentioned:

 send_file( '/Users/my_name/Desktop/image.jpg',
            :disposition => 'inline',
            :type => 'image/jpeg',
            :stream => false,
            :filename => 'image.jpg' )

The :filename is what will be suggested if the browser offers to

save the image

           :disposition => 'inline',
           :type => 'image/png',
           :filename => "on-the-fly.png")

The URI in the needs to be routed to a controller
action of your that finds or builds the image data and uses send_* to
give it to the browser.


Rob B.
[email protected]

Thanks for the help everyone. I ended up using “send_file” as Rob
showed above to get my project to display images in the browser that
are outside my Rails project root.


On Jun 12, 6:37 am, Rob B. [email protected]