# Image uploads

**URL:** <https://www.ruby-forum.com/t/image-uploads/75332>\
**Category:** Rails\
**Created:** [November 9, 2006, 7:44am UTC](https://www.ruby-forum.com/t/image-uploads/75332 "2006-11-09T07:44:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![milano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bcef8e/32.png) [@milano](https://www.ruby-forum.com/u/milano)\
**Post date:** [November 9, 2006, 7:44am UTC](https://www.ruby-forum.com/t/image-uploads/75332/1 "2006-11-09T07:44:30Z")

</div>

I, too, am toying with image uploads… and have got to the stage where  
I can now both save them to DB and resize them using RMagick…

I’m intrigued though by what seems the standard practice in Rails of  
first saving the image to the DB as a blob. Is this required? And what  
are the advantages of doing so? Or is it part of having an image  
“model”?

And what about the recieved wisdom on where to store the images?  
Ideally, I’d like them somewhere in the /public/images… heirarchy  
where they can be accessed from the front end. Is that the right place?  
But in that case, I’m having problems accessing them from my upload form  
(or rather, show.rhtml). If I do something like:

\<img src="\<%= url\_for(:action =\> “picture”, :id =\> @picture.id) %\>"/\>

then it searches for the image in (something like)

/upload/picture/29

which is not at all where me “picture” is.

Can anyone enlighten me?

many thanks

---

<div class="post-metadata">

**Author:** ![milano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bcef8e/32.png) [@milano](https://www.ruby-forum.com/u/milano)\
**Post date:** [November 9, 2006, 7:49am UTC](https://www.ruby-forum.com/t/image-uploads/75332/2 "2006-11-09T07:49:13Z")

</div>

That sounds like file\_column. I was playing with that yesterday. It  
looks in “picture/29” because the id for the image’s entry in the  
database is “29”. The plugin saves each file in a separate folder  
because it doesn’t rename the files and wants to avoid overwriting a  
file with the same name. I didn’t like that much either.

Now if I can just get Flex\_image installed without fryig WEBrick…

---

<div class="post-metadata">

**Author:** ![milano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bcef8e/32.png) [@milano](https://www.ruby-forum.com/u/milano)\
**Post date:** [November 9, 2006, 8:18am UTC](https://www.ruby-forum.com/t/image-uploads/75332/3 "2006-11-09T08:18:51Z")

</div>

Yeah! That’s exactly what I’m reading through right now.

I just installed DarwinPorts and it doesn’t seem to be working. I think  
I need to restart the whole computer to get it going.

I may have a question or two for you once I get to that bashrc file.

cheers  
sean

---

<div class="post-metadata">

**Author:** ![milano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bcef8e/32.png) [@milano](https://www.ruby-forum.com/u/milano)\
**Post date:** [November 9, 2006, 8:39am UTC](https://www.ruby-forum.com/t/image-uploads/75332/4 "2006-11-09T08:39:48Z")

</div>

The main advantages of storing images in database for me is that:

- images are treated the same way as any other data
- no need to juggle with file system operations (especially deleting  
image files from filesystem)
- very easy to move and backup data (just backup database, and not  
database _plus_ images directory)

I personally use FlexImage, which in turn relies on RMagick, which in  
turn requires either GraphicsMagick or ImageMagick to be installed,  
which in turn… 🙂 But once you build the whole chain at least once,  
this is actually the easiest setup I have ever used.

Also, kind of off-topic, but I’ve heard you’re better of as Mongrel as  
a web-server, especially during the development period. There were  
stories when Webrick didn’t reload changed files and stuff like that…  
Myself, I use Mongrel on both development (plain mongrel) and  
production (mongrel cluster) systems.

---

<div class="post-metadata">

**Author:** ![milano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bcef8e/32.png) [@milano](https://www.ruby-forum.com/u/milano)\
**Post date:** [November 9, 2006, 8:13am UTC](https://www.ruby-forum.com/t/image-uploads/75332/5 "2006-11-09T08:13:38Z")

</div>

Thanks Sean,

> That sounds like file\_column.

Yes, sounds like you’re right. And a quick search for file\_column  
returns a wealth of info.

> It looks in “picture/29” because the id for the image’s entry in the  
> database is “29”. The plugin saves each file in a separate folder  
> because it doesn’t rename the files and wants to avoid overwriting a  
> file with the same name.

Actually, that’s fine for me.

> Now if I can just get Flex\_image installed without fryig WEBrick…

Good luck!

You’re on Mac OSX right? Incidentally, I installed RMagick last week  
following this:

[http://rmagick.rubyforge.org/install-osx.html](http://rmagick.rubyforge.org/install-osx.html)

It worked fine. The only hiccup I had was on the actual RMagick  
installation - where I found I had to specify the PATH before it would  
work ( it had installed ImageMagick in a dir called /opt). I also had to  
edit my bashrc file. But once I’d done that, it worked fine. It did take  
a couple of hours though.

---

<div class="post-metadata">

**Author:** ![milano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bcef8e/32.png) [@milano](https://www.ruby-forum.com/u/milano)\
**Post date:** [November 9, 2006, 9:53am UTC](https://www.ruby-forum.com/t/image-uploads/75332/6 "2006-11-09T09:53:10Z")

</div>

MacD wrote:

> The main advantages of storing images in database for me is that:
> 
> - images are treated the same way as any other data
> - no need to juggle with file system operations (especially deleting  
> image files from filesystem)
> - very easy to move and backup data (just backup database, and not  
> database _plus_ images directory)

All very valid reasons. Hadn’t thought of any of them before. Gives me  
something to think about…

> I personally use FlexImage, which in turn relies on RMagick, which in

> Also, kind of off-topic, but I’ve heard you’re better of as Mongrel as  
> a web-server,

I don’t know either, but you convince me to look into both. Thank you.

D

---

<div class="post-metadata">

**Author:** ![milano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bcef8e/32.png) [@milano](https://www.ruby-forum.com/u/milano)\
**Post date:** [November 9, 2006, 11:02am UTC](https://www.ruby-forum.com/t/image-uploads/75332/7 "2006-11-09T11:02:11Z")

</div>

Probably MacD will be able to help you better… but this is what I was  
doing. (Basically I was just following one of the recipes in Chad  
Fowler’s very worthwhile “Rails Recipes” book). I simply put the  
following code into the view:

\<% if @images.any? %\>

## \<%= pluralize(@images.size, "Image") %\>

[\<%= link\_to("Destroy All",:action=\>'clean') %\>]

 \<% @images.each\_with\_index do |image,index| %\> \<%= thumbnail image %\> \<% if (index + 1) % 5 == 0 %\>  
\<% end %\> \<% end %\>
\<% end %\>

It worked fine - but for some reason only returned the top part of the  
image - and obfuscating the lower. The reason for which I haven’t yet  
got to the bottom of - maybe MacD or someone else will know

---

<div class="post-metadata">

**Author:** ![milano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bcef8e/32.png) [@milano](https://www.ruby-forum.com/u/milano)\
**Post date:** [November 9, 2006, 9:50am UTC](https://www.ruby-forum.com/t/image-uploads/75332/8 "2006-11-09T09:50:26Z")

</div>

> I may have a question or two for you once I get to that bashrc file.

Sure. All it really does is set your path - to save you having to do  
something like this:

export PATH=$PATH:/usr/local/bin

every time you run a command.

You can write it directly into a (hidden) file called just .bashrc in  
your home directory. To see if you’ve got that file already open a shell  
and type:

ls -a

If you’ve got TextMate (which most Rails folks seem to use) and you’ve  
set the TextMate command (Can’t remember the details of where to put  
this - somewhere in /usr - Google around for the exact details) you can  
just do:

mate .bashrc

in your shell and it will open the file for you to edit.

This may be the problem with not having Darwin Ports. Maybe it can’t  
find the PATH. I seem to remember I ran into problems here

sudo port -d selfupdate

for exactly this reason. Try and find where it added the command “port”

- then add that to your PATH.

Hope that helps…

---

<div class="post-metadata">

**Author:** ![milano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bcef8e/32.png) [@milano](https://www.ruby-forum.com/u/milano)\
**Post date:** [November 9, 2006, 10:40am UTC](https://www.ruby-forum.com/t/image-uploads/75332/9 "2006-11-09T10:40:16Z")

</div>

> This may be the problem with not having Darwin Ports. Maybe it can’t  
> find the PATH. I seem to remember I ran into problems here
> 
> sudo port -d selfupdate
> 
> for exactly this reason. Try and find where it added the command “port”
> 
> - then add that to your PATH.

That’s exactly it. I restarted and nothing changed, so I fumed and  
snorted for a good twenty minutes before having one of those “Eureka”  
moments. Five minutes later, it was running perfectly.

I have a question about storing images directly in the database. Maybe  
you can help me, MacD. I, too, thought this was a better idea, but never  
figured out how to get them _out_. How do you trick the user’s browser  
into rendering a file that doesn’t actually exist on the server?
