Can anyone point me to a howto that shows the absolute minimum needed for file uploads to a database binary column using native RoR? I''ve googled around, and all the results seem to be about plugins like paperclip or attachment_fu, or complex examples that don''t seem to address a very basic need. I currently have a schema with: create_table "attachments", :force => true do |t| t.integer "user_id" t.integer "property_id" t.binary "file" t.datetime "created_at" t.datetime "updated_at" end and a scaffolded controller with a view containing: <% form_for @attachment, :html => {:multipart => true} do |f| %> <%= f.error_messages %> <!-- other stuff snipped --> <%= f.label :file %><br /> <%= f.file_field :file %> </p> <p> <%= f.submit ''Create'' %> </p> <% end %> Uploading a file tells me: NoMethodError in AttachmentsController#create private method `gsub'' called for #<File:/tmp/RackMultipart20091116-1557-wrrxpn-0> but I can''t find *any* useful documentation as to what needs to go into the create action to pull it all together. I don''t want to do anything complicated; I just want to store the binary in the database for later retrieval. -- "Oh, look: rocks!" -- Doctor Who, "Destiny of the Daleks"
> Can anyone point me to a howto that shows the absolute minimum needed > for file uploads to a database binary column using native RoR? I''ve > googled around, and all the results seem to be about plugins like > paperclip or attachment_fu, or complex examples that don''t seem to > address a very basic need. > > I currently have a schema with: > > create_table "attachments", :force => true do |t| > t.integer "user_id" > t.integer "property_id" > t.binary "file" > t.datetime "created_at" > t.datetime "updated_at" > end > > and a scaffolded controller with a view containing: > > <% form_for @attachment, :html => {:multipart => true} do |f| %> > <%= f.error_messages %> > <!-- other stuff snipped --> > <%= f.label :file %><br /> > <%= f.file_field :file %> > </p> > <p> > <%= f.submit ''Create'' %> > </p> > <% end %> > > Uploading a file tells me: > > NoMethodError in AttachmentsController#create > private method `gsub'' called for > #<File:/tmp/RackMultipart20091116-1557-wrrxpn-0>You can''t just assign a File object to a field in the database. You need to take care of reading the file into a string and putting that into the database. That all said... seriously look at paperclip. It handles all of this for you. Not sure if it has a "db storage" option, but it wouldn''t be hard to write one.> but I can''t find *any* useful documentation as to what needs to go > into > the create action to pull it all together. I don''t want to do anything > complicated; I just want to store the binary in the database for later > retrieval.
Putting files in databases is not a good idea I would say..my opinion at least.. On Mon, Nov 16, 2009 at 11:28 PM, Philip Hallstrom <philip-LSG90OXdqQE@public.gmane.org> wrote:> > > Can anyone point me to a howto that shows the absolute minimum needed > > for file uploads to a database binary column using native RoR? I''ve > > googled around, and all the results seem to be about plugins like > > paperclip or attachment_fu, or complex examples that don''t seem to > > address a very basic need. > > > > I currently have a schema with: > > > > create_table "attachments", :force => true do |t| > > t.integer "user_id" > > t.integer "property_id" > > t.binary "file" > > t.datetime "created_at" > > t.datetime "updated_at" > > end > > > > and a scaffolded controller with a view containing: > > > > <% form_for @attachment, :html => {:multipart => true} do |f| %> > > <%= f.error_messages %> > > <!-- other stuff snipped --> > > <%= f.label :file %><br /> > > <%= f.file_field :file %> > > </p> > > <p> > > <%= f.submit ''Create'' %> > > </p> > > <% end %> > > > > Uploading a file tells me: > > > > NoMethodError in AttachmentsController#create > > private method `gsub'' called for > > #<File:/tmp/RackMultipart20091116-1557-wrrxpn-0> > > You can''t just assign a File object to a field in the database. You > need to take care of reading the file into a string and putting that > into the database. > > That all said... seriously look at paperclip. It handles all of this > for you. Not sure if it has a "db storage" option, but it wouldn''t be > hard to write one. > > > > but I can''t find *any* useful documentation as to what needs to go > > into > > the create action to pull it all together. I don''t want to do anything > > complicated; I just want to store the binary in the database for later > > retrieval. > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Take a look at http://github.com/thoughtbot/paperclip Darian Shimy -- http:/www.darianshimy.com http://twitter.com/dshimy On Mon, Nov 16, 2009 at 12:19 PM, Todd A. Jacobs < tjacobs-sndr-b4faac-S/bPM5e9wgfNLxjTenLetw@public.gmane.org> wrote:> > Can anyone point me to a howto that shows the absolute minimum needed > for file uploads to a database binary column using native RoR? I''ve > googled around, and all the results seem to be about plugins like > paperclip or attachment_fu, or complex examples that don''t seem to > address a very basic need. > > I currently have a schema with: > > create_table "attachments", :force => true do |t| > t.integer "user_id" > t.integer "property_id" > t.binary "file" > t.datetime "created_at" > t.datetime "updated_at" > end > > and a scaffolded controller with a view containing: > > <% form_for @attachment, :html => {:multipart => true} do |f| %> > <%= f.error_messages %> > <!-- other stuff snipped --> > <%= f.label :file %><br /> > <%= f.file_field :file %> > </p> > <p> > <%= f.submit ''Create'' %> > </p> > <% end %> > > Uploading a file tells me: > > NoMethodError in AttachmentsController#create > private method `gsub'' called for > #<File:/tmp/RackMultipart20091116-1557-wrrxpn-0> > > but I can''t find *any* useful documentation as to what needs to go into > the create action to pull it all together. I don''t want to do anything > complicated; I just want to store the binary in the database for later > retrieval. > > -- > "Oh, look: rocks!" > -- Doctor Who, "Destiny of the Daleks" > > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 16, 2009, at 5:23 PM, Darian Shimy wrote:> Take a look at http://github.com/thoughtbot/paperclip > > Darian Shimy > -- > http:/www.darianshimy.com > http://twitter.com/dshimy > > > On Mon, Nov 16, 2009 at 12:19 PM, Todd A. Jacobs <tjacobs-sndr-b4faac-S/bPM5e9wgfNLxjTenLetw@public.gmane.org > > wrote: > > Can anyone point me to a howto that shows the absolute minimum needed > for file uploads to a database binary column using native RoR? I''ve > googled around, and all the results seem to be about plugins like > paperclip or attachment_fu, or complex examples that don''t seem to > address a very basic need. > > I currently have a schema with: > > create_table "attachments", :force => true do |t| > t.integer "user_id" > t.integer "property_id" > t.binary "file" > t.datetime "created_at" > t.datetime "updated_at" > end > > and a scaffolded controller with a view containing: > > <% form_for @attachment, :html => {:multipart => true} do |f| %> > <%= f.error_messages %> > <!-- other stuff snipped --> > <%= f.label :file %><br /> > <%= f.file_field :file %> > </p> > <p> > <%= f.submit ''Create'' %> > </p> > <% end %> > > Uploading a file tells me: > > NoMethodError in AttachmentsController#create > private method `gsub'' called for > #<File:/tmp/RackMultipart20091116-1557-wrrxpn-0> > > but I can''t find *any* useful documentation as to what needs to go > into > the create action to pull it all together. I don''t want to do anything > complicated; I just want to store the binary in the database for later > retrieval. > > -- > "Oh, look: rocks!" > -- Doctor Who, "Destiny of the Daleks"if params[:attachment][:file].blank? flash[:error] = "No file selected for upload" redirect_to :action => ''new'' and return end content = params[:attachment][:file].read if content.blank? flash[:error] = "Selected upload file was empty" redirect_to :action => ''new'' and return end # do something with content @attachment.file = content This code is provided as a public service and comes with *NO WARRANTY* I just want this thread to go away and the answers you''re getting are paying attention to what you asked. Note that this code came from an old project that was Rails 1.1.6 (yeah, *that old*) and my "do something" was to hand off to a BackgrounDRb task to do the processing not really put the content into the database. YMMV -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---