# Displaying images from database

**URL:** <https://www.ruby-forum.com/t/displaying-images-from-database/76796>\
**Category:** Rails\
**Created:** [November 21, 2006, 7:25am UTC](https://www.ruby-forum.com/t/displaying-images-from-database/76796 "2006-11-21T07:25:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [November 21, 2006, 7:25am UTC](https://www.ruby-forum.com/t/displaying-images-from-database/76796/1 "2006-11-21T07:25:37Z")

</div>

I’m saving images to a database when uploaded, and I’m trying to display  
the images back out in the views. I can get this to work, but it’s not  
very consistent… Here’s what I’m doing:

\<%= image\_tag(url\_for(:controller=\>“picture”, :action=\>“show”,  
:id=\>@house.picture\_id),:alt=\>"")%\>

Right off, it doesn’t show the images. But eventually it does, like if  
I interact with the database again, or re-upload an image, they all seem  
to work. This seems to be my problem, when right-clicking on the  
properties of the image, this is the url:

/picture/show/1.png?

And it should be:

/picture/show/1

How come this is appending “.png?” to the end of my image? Should this  
be doing this? Please help, as the images HAVE to show up, and I’ve  
tried everything I can think of. Thanks!

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [November 21, 2006, 8:18am UTC](https://www.ruby-forum.com/t/displaying-images-from-database/76796/2 "2006-11-21T08:18:17Z")

</div>

ryan wrote:

> I’m saving images to a database when uploaded, and I’m trying to display  
> the images back out in the views. I can get this to work, but it’s not  
> very consistent… Here’s what I’m doing:
> 
> \<%= image\_tag(url\_for(:controller=\>“picture”, :action=\>“show”,  
> :id=\>@house.picture\_id),:alt=\>“”)%\>
> 
> Right off, it doesn’t show the images. But eventually it does, like if  
> I interact with the database again, or re-upload an image, they all seem  
> to work. This seems to be my problem, when right-clicking on the  
> properties of the image, this is the url:
> 
> /picture/show/1.png?
> 
> And it should be:
> 
> /picture/show/1
> 
> How come this is appending “.png?” to the end of my image? Should this  
> be doing this? Please help, as the images HAVE to show up, and I’ve  
> tried everything I can think of. Thanks!

The image\_tag helper will append .png if there is no extension in the  
url since it thinks all of your image should be files.

The best way around this is create a custom route that looks like an  
image file.

map.picture ‘pictures/show/:id/imge.jpg’,  
:controller =\> :pictures, :action =\> 'show

or on edge rails:

map.picture ‘pictures/show/:id.jpg’  
:controller =\> :pictures, :action =\> 'show

Then you can:

\<%= image\_tag(picture\_url(:id =\> 1)) %\>

And all should be dandy.

Also look into the Flex Image plugin as it will make the reading and  
writing of image to your database super easy.

[http://www.agilewebdevelopment.com/plugins/fleximage](http://www.agilewebdevelopment.com/plugins/fleximage)

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [November 22, 2006, 2:19am UTC](https://www.ruby-forum.com/t/displaying-images-from-database/76796/3 "2006-11-22T02:19:39Z")

</div>

Is there any way to emulate that :id.jpg routing functionality without  
edge rails? I can’t justify edge for only that, but would really like  
.jpg extensions on the images.

Thanks,  
Chad

On Nov 20, 11:18 pm, Alex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [November 22, 2006, 7:54am UTC](https://www.ruby-forum.com/t/displaying-images-from-database/76796/4 "2006-11-22T07:54:17Z")

</div>

Chad wrote:

> Is there any way to emulate that :id.jpg routing functionality without  
> edge rails? I can’t justify edge for only that, but would really like  
> .jpg extensions on the images.
> 
> Thanks,  
> Chad
> 
> On Nov 20, 11:18 pm, Alex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

You can do the first option I suggested:

map.picture ‘pictures/show/:id/image.jpg’,  
:controller =\> :pictures, :action =\> 'show

it will just give urls like: /pictures/show/1/image.jpg

instead my second example on edge: /pictures/show/1.jpg

It works without edge rails fine, its just not quite as concise.
