Saving and retrieving pictures to a database with rails

I’m developing an application that requires me to save pictures to a
database with other data. I’ve been searching for hours and tried a
few examples, but I keep running into trouble. Can someone recommend a
working example or explain what needs to happen to make this work.
Thank you.

Lucas wrote:

I’m developing an application that requires me to save pictures to a
database with other data. I’ve been searching for hours and tried a
few examples, but I keep running into trouble. Can someone recommend a
working example or explain what needs to happen to make this work.
Thank you.

Here are some pieces that may help to get you started.

In a controller method:


image = params[:listing_image]
File.open("#{temp_file_path}", “w”) do |f|
f.write(image.read)
end

Invoke Imagemagick to resize (could use RMagick if desired)

convert -resize 480x360 #{temp_file_path} #{file_path}
convert -resize 180x135 #{file_path} #{thumb_path}
File.delete(temp_file_path)

In a view (a key is using :multipart => true in the form)


<% form_tag({ :controller => ‘image’, :action => ‘manage_images’, :id =>
@container, :container => “#{@container.class.name}”}, { :id =>
‘editAdForm’, :name => ‘editAdForm’, :multipart => true }) do -%>

<%= file_field_tag(‘listing_image’) %>

Brian A. wrote:

In a controller method:

In a view (a key is using :multipart => true in the form)


<% form_tag({ :controller => ‘image’, :action => ‘manage_images’, :id =>
@container, :container => “#{@container.class.name}”}, { :id =>
‘editAdForm’, :name => ‘editAdForm’, :multipart => true }) do -%>

<%= file_field_tag(‘listing_image’) %>

This method stores the image in a file and a URL representing the image
in the database. If you want to store the actual image bits in the db,
you’ll need to probably store the output of image.read in a blob column

  • may or may not be a good idea depending on your requirements…

Hello,

On 14 Mar 2007, at 04:45, Lucas wrote:

I’m developing an application that requires me to save pictures to a
database with other data. I’ve been searching for hours and tried a
few examples, but I keep running into trouble. Can someone recommend a
working example or explain what needs to happen to make this work.
Thank you.

This might help you:

http://clarkware.com/cgi/blosxom/2007/02/24#FileUploadFu

It’s a tutorial on using Rick O.'s attachment_fu plugin. It can
save images to a database, file system or Amazon S3.

Regards,
Andy S.

Please be a bit more specific with your approach and the troubles.
With any application, detailed requirements are important.

Saving pictures to a database? Think of them as files saved in the
database. binary blobs with names.
With Rails or any other framework, a database needs planning. (the
reason David Black gives SQL examples in Ruby for Rails, because you
do need to be in control of the database. It defines and limits data
structures. )

You want to think about your database.
What tables and data types it needs, how they’re related. There isn’t
one approach.

table images
column blob
column id
column other data