In my app, the user can upload images. Rather than shoving the image data in the database, i want to put the filename and content type into the db, then rename the image with its record''s id number, and then save it into a local folder in my app folder. (hardcoded for now) All the details are being saved to the db ok, so as far as i know the "picture_file=" method is fine. But when i try to write the image with write_to_disc i have a problem: if i get the ''read'' field from the uploaded image, store it in a variable, and try to write it with another method, i get an error saying "no ''write'' method exists for String": can anyone see what i''m doing wrong here? (i''ve just enclosed the model methods as all the controller does is call ''write_to_disc'' on my picture instance): #in class Picture attr_accessor :file_data def picture_file=(input_data) self.filename = input_data.original_filename self.content_type = input_data.content_type.chomp self.file_data = input_data.read end def write_to_disc #save the picture to newspipe/data/pictures with the name ''#{self.id}.extension'' new_filename = "#{self.id}.#{self.content_type.split("/").last}" if self.file_data.write("C:\\code\\InstantRails\\rails_apps\\newspipe\\data\\pictures\\data\\pictures\\#{new_filename}") return true else return false end end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You need to create StringIO or TempFile object for storing input_data On 8/3/07, Max Williams <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > In my app, the user can upload images. Rather than shoving the image > data in the database, i want to put the filename and content type into > the db, then rename the image with its record''s id number, and then save > it into a local folder in my app folder. (hardcoded for now) > > All the details are being saved to the db ok, so as far as i know the > "picture_file=" method is fine. But when i try to write the image with > write_to_disc i have a problem: if i get the ''read'' field from the > uploaded image, store it in a variable, and try to write it with another > method, i get an error saying "no ''write'' method exists for String": > can anyone see what i''m doing wrong here? (i''ve just enclosed the > model methods as all the controller does is call ''write_to_disc'' on my > picture instance): > > #in class Picture > > attr_accessor :file_data > > def picture_file=(input_data) > self.filename = input_data.original_filename > self.content_type = input_data.content_type.chomp > self.file_data = input_data.read > end > > def write_to_disc > #save the picture to newspipe/data/pictures with the name > ''#{self.id}.extension'' > new_filename = "#{self.id}.#{self.content_type.split("/").last}" > if > self.file_data.write("C:\\code\\InstantRails\\rails_apps\\newspipe\\data\\pictures\\data\\pictures\\#{new_filename}") > return true > else > return false > end > end > -- > Posted via http://www.ruby-forum.com/. > > > >-- Cheers! - Pratik http://m.onkey.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 -~----------~----~----~----~------~----~------~--~---
Pratik Naik wrote:> You need to create StringIO or TempFile object for storing input_data >Thanks Pratik - i just tried that after reading your post, but probably did it wrong - i get a complaint about "uninitialized constant Picture::TempFile" (i''ve done require ''tempfile'' in the model by the way) The first method is causing the crash now - am i using TempFile wrong? def picture_file=(input_data) self.filename = input_data.original_filename self.content_type = input_data.content_type.chomp self.file_data = TempFile.new(input_data) end def write_to_disc #save the picture to newspipe/data/pictures with the name ''#{self.id}.extension'' new_filename = "pic#{self.id}.#{self.content_type.split("/").last}" self.file_data.write("C:/code/InstantRails/rails_apps/newspipe/data/pictures/#{new_filename}") end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Umm...try self.file_data = ::TempFile.new(input_data) On 8/3/07, Max Williams <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Pratik Naik wrote: > > You need to create StringIO or TempFile object for storing input_data > > > > Thanks Pratik - i just tried that after reading your post, but probably > did it wrong - i get a complaint about "uninitialized constant > Picture::TempFile" (i''ve done require ''tempfile'' in the model by the > way) > > The first method is causing the crash now - am i using TempFile wrong? > > def picture_file=(input_data) > self.filename = input_data.original_filename > self.content_type = input_data.content_type.chomp > self.file_data = TempFile.new(input_data) > end > > def write_to_disc > #save the picture to newspipe/data/pictures with the name > ''#{self.id}.extension'' > new_filename = "pic#{self.id}.#{self.content_type.split("/").last}" > self.file_data.write("C:/code/InstantRails/rails_apps/newspipe/data/pictures/#{new_filename}") > end > -- > Posted via http://www.ruby-forum.com/. > > > >-- Cheers! - Pratik http://m.onkey.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 -~----------~----~----~----~------~----~------~--~---
Pratik Naik wrote:> Umm...try > > self.file_data = ::TempFile.new(input_data) >done that - i''m still getting "uninitialized constant TempFile" back though. thanks for the help by the way... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
tyler.a.montgomery-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Aug-04 16:53 UTC
Re: Problems saving an uploaded image to an app folder
http://manuals.rubyonrails.com/read/chapter/56 this helped me get files into the file system using: File.open("#{RAILS_ROOT}/path/to/storage/#{new_filename}.jpg", "wb") do |f| f.write(@params[''picture_file''].read) end then you can access all the files from within your app instead of writing to your local disk and of course you can change the file extension to take any kind of picture --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
tyler.a.montgomery-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> http://manuals.rubyonrails.com/read/chapter/56 > > this helped me get files into the file system using: > > File.open("#{RAILS_ROOT}/path/to/storage/#{new_filename}.jpg", "wb") > do |f| > f.write(@params[''picture_file''].read) > end > > then you can access all the files from within your app instead of > writing to your local disk > > and of course you can change the file extension to take any kind of > pictureThat''s great, thanks tyler! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---