Image Upload

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 :slight_smile:

Regards,
Robert

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 :slight_smile:

Regards,
Robert

Try looking at the FlexImage plugin:
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

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.

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

Yeah - the only thing missing then is validation :slight_smile: