# Display pictures in database

**URL:** <https://www.ruby-forum.com/t/display-pictures-in-database/81172>\
**Category:** Rails\
**Created:** [January 5, 2007, 8:36pm UTC](https://www.ruby-forum.com/t/display-pictures-in-database/81172 "2007-01-05T20:36:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [January 5, 2007, 8:36pm UTC](https://www.ruby-forum.com/t/display-pictures-in-database/81172/1 "2007-01-05T20:36:55Z")

</div>

I can upload pictures to a database, then display all pictures via the  
filename, the files are currently stored on the filesystem…

for picture in @pictures  
-%\>

\<%=image\_tag(picture.name, :size=\>"50x50", :id=\>"side\_image\_" + indx.to\_s, :style=\>"border:0;", :alt=\>"Images")-%\> \<%indx = indx + 1-%\>

 \<% end %\>

I changed the code to upload images into a BLOB field in the database  
cause I don’t want to use the filesystem to serve up images any longer.  
How can I change this rhtml code so that it goes to the BLOB field in  
the database and display the pictures?

This is the code that builds the picture array,

def find\_8\_pics  
total\_pics = Picture.count  
@page\_num = @params[:page\_num].to\_i  
if(@page\_num \< 0 or @page\_num \* 8 \>= total\_pics)  
@page\_num = 0  
end  
offset = @page\_num \* 8

```
if(offset + 9 <= total_pics)
  @next_page = @page_num + 1
end

if(@page_num != 0)
  @prev_page = @page_num - 1;
end

@pictures = Picture.find :all, :limit=>8, :offset=>offset
@blank_images = 8 - @pictures.length

```

end

Thanks,  
Peter

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [January 5, 2007, 10:54pm UTC](https://www.ruby-forum.com/t/display-pictures-in-database/81172/2 "2007-01-05T22:54:35Z")

</div>

Alex W. wrote:

> Peter wrote:
> 
> > I changed the code to upload images into a BLOB field in the database  
> > cause I don’t want to use the filesystem to serve up images any longer.  
> > How can I change this rhtml code so that it goes to the BLOB field in  
> > the database and display the pictures?
> 
> You need to create an action that sends along the image data.
> 
> def image  
> @picture = Picture.find(params[:id)  
> send\_data @picture.data, :content\_type =\> ‘image/jpeg’  
> end
> 
> Then just call the url for that action as the image url
> 
> \<%= image\_tag url\_for(:action =\> ‘image’, :id =\> 123) %\>
> 
> Also check out FlexImage:  
> [http://www.agilewebdevelopment.com/plugins/fleximage](http://www.agilewebdevelopment.com/plugins/fleximage)
> 
> It may make things easier for you.

I got that part thanks. Now my problem is that I have a thumbnail that,  
when hovered over, populates a larger area in the center. The problem  
now is that the hover javascript references the .src counter value for  
the picture being hovered over to correctly identify the  
id(side\_image\_0)…with the files on disk, this works…

\<span onmouseover=“set\_selected\_pic(0);”  
 ![Images](https://www.ruby-forum.com/picture/Larry.jpg)span\>

…from the database src becomes /studio/picture/15, which doesn’t work  
with the hover…I tried setting alt="/studio/picture/Larry.jpg but  
can’t get that working…

\<span onmouseover=“set\_selected\_pic(0);”  
 ![/studio/picture/Larry.jpg](https://www.ruby-forum.com/studio/picture/15)span\>

this is the hover javascript,

function set\_selected\_pic(i)  
{  
$(‘side\_image\_’ + cur\_selection).style.border = ‘0px’;  
$(‘side\_image\_’ + i).style.border = ‘2px solid black’;  
$(‘rollimg’).src = $(‘side\_image\_’ + i).src;  
cur\_selection = i;  
}

How can I populate the center pane with the image being hovered over  
when src = “/studio/picture/15”

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [January 5, 2007, 8:56pm UTC](https://www.ruby-forum.com/t/display-pictures-in-database/81172/3 "2007-01-05T20:56:24Z")

</div>

Peter wrote:

> I changed the code to upload images into a BLOB field in the database  
> cause I don’t want to use the filesystem to serve up images any longer.  
> How can I change this rhtml code so that it goes to the BLOB field in  
> the database and display the pictures?

You need to create an action that sends along the image data.

def image  
@picture = Picture.find(params[:id)  
send\_data @picture.data, :content\_type =\> ‘image/jpeg’  
end

Then just call the url for that action as the image url

\<%= image\_tag url\_for(:action =\> ‘image’, :id =\> 123) %\>

Also check out FlexImage:  
[http://www.agilewebdevelopment.com/plugins/fleximage](http://www.agilewebdevelopment.com/plugins/fleximage)

It may make things easier for you.
