# Editing FlexImage filenames

**URL:** <https://www.ruby-forum.com/t/editing-fleximage-filenames/81015>\
**Category:** Rails\
**Created:** [January 3, 2007, 1:52am UTC](https://www.ruby-forum.com/t/editing-fleximage-filenames/81015 "2007-01-03T01:52:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [January 3, 2007, 1:52am UTC](https://www.ruby-forum.com/t/editing-fleximage-filenames/81015/1 "2007-01-03T01:52:42Z")

</div>

I’m using FlexImage to upload files, and I want to also be able to go  
back and edit the file.

So an edit operation would produce the name of the file that was  
uploaded, and produce a thumbnail for visual reference, with the ability  
to delete this image entirely, or to change it’s uploaded path/image.

Would I need to add a column to my Images table to save the uploaded  
path, and then populate the file\_field with that value? (Is there even a  
way to populate the file\_field with a previous value?)

Any thoughts or experiences would be time saving and appreciated.

Matt

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [January 3, 2007, 7:37am UTC](https://www.ruby-forum.com/t/editing-fleximage-filenames/81015/2 "2007-01-03T07:37:25Z")

</div>

OK. different question, but I’ll keep it in the same thread, since Its  
FlexImage related.

What is the best way to display multiple images that are already in the  
database from a FlexImage upload?

I have multiple photos that belong to one listing (one-to-many), I was  
going to do a render\_flex\_image() on one of the images, but I’m getting  
an error back that it doesn’t like the datatype I’m handing in (Array)

@listing = Listing.find(9)  
@image = Photo.find(:all,  
:conditions =\> [“listing\_id = ?”, @listing.id] )

# @image is an array of 1 image

render\_flex\_image(@image)  
–\> undefined method `data\_field’ for Array:Class

If I use the following instead:  
render\_flex\_image(@image[0])

I don’t get an error, and I get the image, but without any layout.

What am I doing wrong?

Ultimately, I just want to display numerous photos on the same page.

Matt

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [January 3, 2007, 9:08pm UTC](https://www.ruby-forum.com/t/editing-fleximage-filenames/81015/3 "2007-01-03T21:08:47Z")

</div>

matt wrote:

> I’m using FlexImage to upload files, and I want to also be able to go  
> back and edit the file.
> 
> So an edit operation would produce the name of the file that was  
> uploaded, and produce a thumbnail for visual reference, with the ability  
> to delete this image entirely, or to change it’s uploaded path/image.

The thumbnail would be created when it’s requested, rather than at  
upload. To create the thumbnail when it’s uploaded, use file\_column  
instead.

> Would I need to add a column to my Images table to save the uploaded  
> path, and then populate the file\_field with that value? (Is there even a  
> way to populate the file\_field with a previous value?)

The filestore is defined once for the entire class. It’s not really  
designed to start moving the stored images around in various places on  
the filesystem. You will have to roll your own solution for that.

I’m not really getting what you are trying to accomplish here. Would it  
make more sense to have a dynamic route that simply requests the proper  
image based on some attribute rather than physically moving the images  
around the filesystem of your server?

> What is the best way to display multiple images that are already in the  
> database from a FlexImage upload?

Remember your HTTP basics here. 1 request from the browser, 1 response  
from the server. You can’t send multiple responses to the same request.  
To put mulptiple images on a page, simply put the html for multiple  
image tags on that page. Then the browser will send a specific request  
for each image

\<% @item.images.each do |img| %\>  
\<%= image\_tag :action =\> ‘image’, :id =\> img %\>  
\<% end %\>

Then your controller gets a request for each image, with that image’s  
id, then sends the image data.

> Can render\_flex\_image() take an Array of images?

No. Since you can only send 1 response per request, you can only render  
1 image per request.

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [January 3, 2007, 4:59pm UTC](https://www.ruby-forum.com/t/editing-fleximage-filenames/81015/4 "2007-01-03T16:59:01Z")

</div>

Can render\_flex\_image() take an Array of images?

What is the proper usage/intent of render\_flex\_image() ?

When I use it, it only displays an image, no layout, is that the proper  
intent?

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [January 3, 2007, 11:22pm UTC](https://www.ruby-forum.com/t/editing-fleximage-filenames/81015/5 "2007-01-03T23:22:15Z")

</div>

matt wrote:

> > > Can render\_flex\_image() take an Array of images?
> > 
> > No. Since you can only send 1 response per request, you can only render  
> > 1 image per request.
> 
> I think this is the crux of my mis-understanding.
> 
> What does render\_flex\_image do?

Normally, rails will send text data in response to a request, in the  
form of HTML. This is created from executing a .rhtml file. The result  
is text data and has a content type of text/html.

Now assuming:

def foo  
@foo = MyImage.find(1)  
render\_flex\_image(@foo)  
end

without the render\_flex\_image, rails would try to run foo.rhtml to  
create the response data. Since there is no template by that name, we  
don’t want that to happen.

What does render\_flex\_image(@foo) do then? It tell rails to send the  
image data (from the object @foo) as the response instead of sending the  
result of a .rhtml template file. The browser receives binary image  
data with a content type of image/jpeg.

Short version: It makes rails send binary image/jpeg data rather  
text/html data.

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [January 3, 2007, 10:16pm UTC](https://www.ruby-forum.com/t/editing-fleximage-filenames/81015/6 "2007-01-03T22:16:29Z")

</div>

> > Can render\_flex\_image() take an Array of images?
> 
> No. Since you can only send 1 response per request, you can only render  
> 1 image per request.

I think this is the crux of my mis-understanding.

What does render\_flex\_image do?

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [January 3, 2007, 11:38pm UTC](https://www.ruby-forum.com/t/editing-fleximage-filenames/81015/7 "2007-01-03T23:38:53Z")

</div>

On Wed, 2007-01-03 at 23:22 +0100, Alex W. wrote:

> Normally, rails will send text data in response to a request, in the  
> without the render\_flex\_image, rails would try to run foo.rhtml to

Thanks for the extended description, I think his explains why the view  
source was garbled. 🙂

So render\_flex\_image really is intended to only display a single image  
in the browser, with no extra markup ?

So if I wanted to display an array of images in a specified layout, I  
could use something like:

render :action =\> ‘display\_images\_with\_layout’

and then have a display\_images\_with\_layout.rhtml file that loops through  
the images per your previous post?

Thanks

Matt

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [January 4, 2007, 12:08am UTC](https://www.ruby-forum.com/t/editing-fleximage-filenames/81015/8 "2007-01-04T00:08:44Z")

</div>

matt wrote:

> On Wed, 2007-01-03 at 23:22 +0100, Alex W. wrote:
> 
> > Normally, rails will send text data in response to a request, in the  
> > without the render\_flex\_image, rails would try to run foo.rhtml to
> 
> Thanks for the extended description, I think his explains why the view  
> source was garbled. 🙂

Image data in a text viewer really isn’t very pretty

> So render\_flex\_image really is intended to only display a single image  
> in the browser, with no extra markup ?

Right. In fact you CAN’T mix markup with image data. The browser  
expects to get html from the first request and then get image data for  
an ![]() tags it finds in that html. If there is any markup  
along wiht that image data, the browser will think it’s an invalid  
format and wont display the image.

> So if I wanted to display an array of images in a specified layout, I  
> could use something like:
> 
> render :action =\> ‘display\_images\_with\_layout’
> 
> and then have a display\_images\_with\_layout.rhtml file that loops through  
> the images per your previous post?

Here’s a full example, assume Foo has\_many :my\_images

#controller  
def show  
@foo = Foo.find(params[:id])  
end

def image  
@image = MyImage.find(params[:id])  
render\_flex\_image(@image)  
end

#show.rhtml

# Images for this foo
 \<% @foo.images.each do |image| %\> \<%= image\_tag url\_for(:action =\> 'image', :id =\> image) %\> \<% end %\>

This creates html like:

# Images for this foo
 ![](https://www.ruby-forum.com/products/image/1) ![](https://www.ruby-forum.com/products/image/5) ![](https://www.ruby-forum.com/products/image/8)

So the browser then gets the image at each of those urls. And your  
browser doesn’t care if those image urls are static files served by the  
webserver or dynamically created by rails.
