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? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Bartek Iwaszkiewicz
2011-Mar-22 03:07 UTC
Re: Tutorial for native Rails 3 upload to 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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: http://www.tutorialspoint.com/ruby-on-rails/rails-file-uploading.htm (first hit when searching for "rails file upload" on google ;)) Best, Sebastian -------------------------- Sebastian Gräßl sebastian-30c1IgTnpsdP52+qxqhqIQ@public.gmane.org http://validcode.me -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.