# Image from database and display

**URL:** <https://www.ruby-forum.com/t/image-from-database-and-display/166897>\
**Category:** Rails\
**Created:** [April 30, 2009, 5:55pm UTC](https://www.ruby-forum.com/t/image-from-database-and-display/166897 "2009-04-30T17:55:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [April 30, 2009, 5:55pm UTC](https://www.ruby-forum.com/t/image-from-database-and-display/166897/1 "2009-04-30T17:55:14Z")

</div>

I have an image saved in a database and am trying to display it in my  
view. Here is my code:

View-  
\<%= image\_tag( url\_for :action =\> ‘image’, :id =\> 1, :pseudo\_id =\>  
@pseudo.id) %\>

Controller-  
@image = Image.find(:first, :conditions =\> [‘enpseudo\_id = ?’,  
@pseudo.id])  
send\_data @img.img, :content\_type =\> ‘image/gif’

What I get is a broken image, but the dimensions look like the correct  
dimensions which makes me thing that it is actually reading the image  
from the database but not displaying it correctly. Thanks in advance,

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [April 30, 2009, 8:22pm UTC](https://www.ruby-forum.com/t/image-from-database-and-display/166897/2 "2009-04-30T20:22:09Z")

</div>

I got it, here is what I did:

View:

\<%= image\_tag("/admin/code\_image/#{@image.id}", :alt =\> “Image”) %\>

Controller - code\_image

def code\_image  
@image\_data = Image.find\_by\_id(params[:id])  
@image = @image\_data.img  
send\_data (@image, :disposition =\> ‘inline’)  
end

Hope this is of some help to somebody.

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [April 30, 2009, 8:31pm UTC](https://www.ruby-forum.com/t/image-from-database-and-display/166897/3 "2009-04-30T20:31:48Z")

</div>

Yes… the image is fetched by the browser as a completely separate  
request, so you need to be able to handle that request.

On Apr 30, 11:22 am, Shandy N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
