I've searched in vain for a basic tutorial on how to upload a binary
file to a database using vanilla Rails 3. Yes, I know about Paperclip;
no, I don't want to upload the files to a filesystem.
What I'd really like to do is capture the filename, mime-type, and
data inside the create/update action--no separate upload action
required, as the table is dedicated to the file uploads.
So far, I have this:
= form_for @photo, :html => { :multipart => true } do |f|
= f.label :data
%br/
= f.file_field :data
.actions
= f.submit
but get festive error messages like:
NoMethodError in PhotosController#create
undefined method `gsub' for #<ActionDispatch::Http::UploadedFile:
0xa64da84>
when I submit the form. What should my create/update actions look like
in order to store the file and metadata properly inside the database?
W dniu 2011-03-22 03:22, Todd A. Jacobs pisze:
I've searched in vain for a basic tutorial on how to upload a binary
file to a database using vanilla Rails 3. Yes, I know about Paperclip;
no, I don't want to upload the files to a filesystem.
What I'd really like to do is capture the filename, mime-type, and
data inside the create/update action--no separate upload action
required, as the table is dedicated to the file uploads.
So far, I have this:
= form_for @photo, :html => { :multipart => true } do |f|
= f.label :data
%br/
= f.file_field :data
.actions
= f.submit
but get festive error messages like:
NoMethodError in PhotosController#create
undefined method `gsub' for #<ActionDispatch::Http::UploadedFile:
0xa64da84>
when I submit the form. What should my create/update actions look like
in order to store the file and metadata properly inside the database?
My basic version is
# view
<%= form_for @photo, :html => {:multipart => true } do
f> %>
<%= f.file_field :upload %>
<%= f.submit "Save" %>
<% end %>
# controller
...
@photo.save
...
# model
attr_accessor :upload</small>
Hej Tod,
the above example is a good start, but you need to keep in mind, that the upload attribute of the photo is actually a File object.
This means you will need to copy it to a permanent location, because in that state it most likely is in the /tmp dir.
Take a look at this tutorial: Ruby on Rails - File Uploading (first hit when searching for “rails file upload” on google ;))
Best,
Sebastian