# Image Upload

**URL:** <https://www.ruby-forum.com/t/image-upload/71817>\
**Category:** Rails\
**Created:** [October 11, 2006, 6:25pm UTC](https://www.ruby-forum.com/t/image-upload/71817 "2006-10-11T18:25:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [October 11, 2006, 6:25pm UTC](https://www.ruby-forum.com/t/image-upload/71817/1 "2006-10-11T18:25:00Z")

</div>

Hi,

assuming I have a db table “Items” like this:

CREATE TABLE items (  
id INTEGER NOT NULL AUTO\_INCREMENT,  
product\_id INTEGER NOT NULL,  
name VARCHAR(30) NOT NULL,  
description VARCHAR(500) NOT NULL,  
imageurl VARCHAR(55),  
imagethumburl VARCHAR(55),  
price FLOAT NOT NULL,  
address\_id INTEGER NOT NULL,  
seller\_contact\_info\_id INTEGER NOT NULL,  
totalscore INTEGER NOT NULL,  
numberofvotes INTEGER NOT NULL,  
primary key (id),  
foreign key (address\_id) references addresses(id),  
foreign key (product\_id) references products(id),  
foreign key (seller\_contact\_info\_id) references  
seller\_contact\_infos(id)  
);

and I want to do the image upload inside a form - can I extend the  
“Item” model in order to let me handle the file upload? Any pointers in  
the right direction would be much appreciated 🙂

Regards,  
Robert

---

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [October 11, 2006, 6:45pm UTC](https://www.ruby-forum.com/t/image-upload/71817/2 "2006-10-11T18:45:50Z")

</div>

Robert S. wrote:

> Hi,
> 
> assuming I have a db table “Items” like this:
> 
> …
> 
> and I want to do the image upload inside a form - can I extend the  
> “Item” model in order to let me handle the file upload? Any pointers in  
> the right direction would be much appreciated 🙂
> 
> Regards,  
> Robert

Try looking at the FlexImage plugin:  
[http://beautifulpixel.com/flex\_image/index.html](http://beautifulpixel.com/flex_image/index.html)

But if you want to roll your own, then you need to add a binary column  
to your database table.

Form:

\<%= form\_tag :action =\> ‘create’ %\>  
\<%= file\_field ‘some\_model’, ‘data’ %\>  
\<%= submit\_tag ‘Upload!’ %\>  
\<%= end\_form\_tag %\>

Controller Action:

def create  
@some\_model = SomeModel.new

```
# must call the read method on uploaded file to convert
# them to strings
@some_model.data = params[:some_model][:data].read
@some_model.save

```

end

---

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [October 12, 2006, 3:12pm UTC](https://www.ruby-forum.com/t/image-upload/71817/3 "2006-10-12T15:12:03Z")

</div>

Thanks for the hint, I’ll look into it. I don’t want to store the image  
into the db though, but as a file on the server.

---

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [October 12, 2006, 6:11pm UTC](https://www.ruby-forum.com/t/image-upload/71817/4 "2006-10-12T18:11:33Z")

</div>

Robert S. wrote:

> Thanks for the hint, I’ll look into it. I don’t want to store the image  
> into the db though, but as a file on the server.

In that case:

#view  
\<%= file\_field\_tag ‘file’ %\>

#controller  
File.open("#{RAILS\_ROOT}/path/to/file") do |f|  
f.write params[:file].read  
end

---

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [October 12, 2006, 6:13pm UTC](https://www.ruby-forum.com/t/image-upload/71817/5 "2006-10-12T18:13:30Z")

</div>

Yeah - the only thing missing then is validation 🙂
