# How-to display an image living outside the public directory

**URL:** <https://www.ruby-forum.com/t/how-to-display-an-image-living-outside-the-public-directory/187990>\
**Category:** Rails\
**Created:** [May 3, 2010, 8:09pm UTC](https://www.ruby-forum.com/t/how-to-display-an-image-living-outside-the-public-directory/187990 "2010-05-03T20:09:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bill\_walton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f07891/32.png) [@bill\_walton](https://www.ruby-forum.com/u/bill_walton)\
**Post date:** [May 3, 2010, 8:09pm UTC](https://www.ruby-forum.com/t/how-to-display-an-image-living-outside-the-public-directory/187990/1 "2010-05-03T20:09:53Z")

</div>

I’m trying to display an image (chart) that lives outside the public  
directory in RAILS\_ROOT/private.

The chart contains confidential information and so can’t live in /public  
or any sub-directories of /public.

The chart needs to be displayed from a partial. The first display of  
the page containing the partial is an html render. Subsequently, the  
user can select a different chart causing an ajax call which replaces  
the content of the image-containing dom element in the partial with a  
new chart image.

Any thoughts / pointers are much appreciated!

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![bill\_walton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f07891/32.png) [@bill\_walton](https://www.ruby-forum.com/u/bill_walton)\
**Post date:** [May 3, 2010, 9:11pm UTC](https://www.ruby-forum.com/t/how-to-display-an-image-living-outside-the-public-directory/187990/2 "2010-05-03T21:11:10Z")

</div>

bill walton wrote:

> I’m trying to display an image (chart) that lives outside the public  
> directory in RAILS\_ROOT/private.

You probably looking for send\_file:

[http://railsapi.com/doc/rails-v2.3.5/classes/ActionController/Streaming.html#M001587](http://railsapi.com/doc/rails-v2.3.5/classes/ActionController/Streaming.html#M001587)

---

<div class="post-metadata">

**Author:** ![bill\_walton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f07891/32.png) [@bill\_walton](https://www.ruby-forum.com/u/bill_walton)\
**Post date:** [May 3, 2010, 11:03pm UTC](https://www.ruby-forum.com/t/how-to-display-an-image-living-outside-the-public-directory/187990/3 "2010-05-03T23:03:45Z")

</div>

Hi Rob,

On Mon, May 3, 2010 at 2:11 PM, Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> bill walton wrote:
> 
> > I’m trying to display an image (chart) that lives outside the public  
> > directory in RAILS\_ROOT/private.
> 
> You probably looking for send\_file:

Yeah. I was trying to use send\_data and it wasn’t working. Not sure  
why,  
but send\_file does. I got it working but gmail doesn’t send my own  
posts to  
me so couldn’t reply that I’d solved it. Thanks much for replying. I  
appreciate it.

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![bill\_walton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f07891/32.png) [@bill\_walton](https://www.ruby-forum.com/u/bill_walton)\
**Post date:** [May 4, 2010, 5:35am UTC](https://www.ruby-forum.com/t/how-to-display-an-image-living-outside-the-public-directory/187990/4 "2010-05-04T05:35:49Z")

</div>

You could have PrivateImagesController with `show` method

and you will get your images through /private\_images/image\_name.png

in `show` method

```
(check permissions)
send_file 

```

“#{RAILS\_ROOT}/private\_images\_folder/#{params[:image\_name]}”

send\_file is really slow and will work only when there’s not a lot of  
users  
requesting files

In Nginx you could use x-send-file option or something, which makes  
send\_file work much better. I didn’t try that though.
