Saving and retrieving pictures to a database with rails.

L

Lucas

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.
 
B

Brian Adkins

Lucas said:
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') %>
....
 
B

Brian Adkins

Brian said:
Lucas said:
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') %>
...

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...
 
J

John Joyce

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
 
A

Andrew Stewart

Hello,

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 Olson's attachment_fu plugin. It can
save images to a database, file system or Amazon S3.

Regards,
Andy Stewart
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top