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):
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
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 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
The first method is causing the crash now - am i using TempFile wrong?
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