Forum: Ruby on Rails Upload image to MySQL

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ead43bcb0202410540a6a7cee5955d46?d=identicon&s=25 Fresh Mix (giga)
on 2009-03-17 21:37
Need simple sample, how to upload image in to MySQL database (in BLOB)

Rails 2.2.2
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-03-17 21:52
(Received via mailing list)
Fresh Mix wrote:
> Need simple sample, how to upload image in to MySQL database (in BLOB)

That's one of the big "Don'ts!" among the "Do's and Don't's of
Websites". There
are many, many reasons not to put images into your database. The very
least of
which - your web server cannot bypass Rails and efficiently serve the
image!

Use Paperclip - it's the best image uploader/storer/server plugin we
have seen
so far...
C4bfcc81ac9281cb905f38e97e4d4e0b?d=identicon&s=25 Shandy Nantz (snantz)
on 2009-03-17 22:32
your web server cannot bypass Rails and efficiently serve the
> image!

I admit that I am by no means a database expert but I implemented this
about two weeks ago and have had no problems (so far).

Here is what I did.

First my field in the adatabase I declared as a bytea (Byte Array). I
tried using other field types, such as OID, with no success.

View:
<% form_remote_tag( :url => {:action => 'uploadFile',
  :account_id =>  @account.id}, :update => 'image_div' ) do -%>

  <p><label for="image_img">Select a File:</label>
  <%= file_field 'image', 'img' %></p>
  <%= submit_tag "Upload" %>
<% end -%>

:controller
  @image = Image.new
  @image.img = File.open(params[:image][:img] , "rb").read
  @image.save

I am not sure what purpose the "rb" serves since I had to look long and
hard for a working example, and in the end I think I was relegated to
trying different approaching from several examples.

:View to render the image - First get image via simple find
  <%= image_tag url_for(:action => 'image', :account_id => @account.id)
%>

Like I said this example renders the image just fine, but I would be
very interested to hear what possible problems there are with this
appraoch as I have done this for a project at work and it is about a
month away from going live. Thanks,

-S
This topic is locked and can not be replied to.