# Displaying an image from the database

**URL:** <https://www.ruby-forum.com/t/displaying-an-image-from-the-database/122705>\
**Category:** Rails\
**Created:** [December 11, 2007, 1:31am UTC](https://www.ruby-forum.com/t/displaying-an-image-from-the-database/122705 "2007-12-11T01:31:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 11, 2007, 1:31am UTC](https://www.ruby-forum.com/t/displaying-an-image-from-the-database/122705/1 "2007-12-11T01:31:09Z")

</div>

Can someone help me display an image from a database?

I am trying to create an image tag for an image located in a  
database. The following action is located in the  
show\_photo\_controller.

def database\_data  
@photo = Photo.find(params[:id])  
send\_data(@photo.data,  
:filename =\> @photo.name,  
:type =\> @photo.content\_type,  
:disposition =\> “inline”)  
end

If I go to the url [http://localhost:3000/show\_photo/database\_data/1](http://localhost:3000/show_photo/database_data/1),  
the photo displays properly. However, when I insert the following in  
my view, it displays the image’s binary as text. Suggestions?

\<%= image\_tag(“/show\_photo/database\_data/1”, :alt =\> “Image”) %\>

I’ve also pasted my code at [Parked at Loopia](http://pastie.caboo.se/126726). Thanks!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 11, 2007, 2:16am UTC](https://www.ruby-forum.com/t/displaying-an-image-from-the-database/122705/2 "2007-12-11T02:16:09Z")

</div>

What happens if you put a .jpg after the 1 out of curiosity?

On Dec 11, 2007 10:51 AM, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> :filename => @photo.name,
> 
> ```
> 
> I’ve also pasted my code at [Parked at Loopia](http://pastie.caboo.se/126726). Thanks!
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 11, 2007, 3:44am UTC](https://www.ruby-forum.com/t/displaying-an-image-from-the-database/122705/3 "2007-12-11T03:44:19Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Can someone help me display an image from a database?
> 
> I am trying to create an image tag for an image located in a  
> database. The following action is located in the  
> show\_photo\_controller.
> 
> def database\_data  
> @photo = Photo.find(params[:id])  
> send\_data(@photo.data,  
> :filename =\> @photo.name,  
> :type =\> @photo.content\_type,  
> :disposition =\> “inline”)  
> end
> 
> If I go to the url [http://localhost:3000/show\_photo/database\_data/1](http://localhost:3000/show_photo/database_data/1),  
> the photo displays properly. However, when I insert the following in  
> my view, it displays the image’s binary as text. Suggestions?
> 
> \<%= image\_tag(“/show\_photo/database\_data/1”, :alt =\> “Image”) %\>
> 
> I’ve also pasted my code at [Parked at Loopia](http://pastie.caboo.se/126726). Thanks!

change it to:  
\<%= image\_tag(“/show\_photo/database\_data?id=1”, :alt =\> “Image”) %\>  
I don’t know why the standard routing doesn’t work in this case, but I  
had same issue.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 11, 2007, 8:11am UTC](https://www.ruby-forum.com/t/displaying-an-image-from-the-database/122705/4 "2007-12-11T08:11:13Z")

</div>

Great! \<%= image\_tag(“/show\_photo/database\_data?id=1”, :alt =\>  
“Image”) %\> worked! Thanks!

On Dec 10, 6:44 pm, Joel N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
