# Problems saving an uploaded image to an app folder

**URL:** <https://www.ruby-forum.com/t/problems-saving-an-uploaded-image-to-an-app-folder/111183>\
**Category:** Rails\
**Created:** [August 3, 2007, 4:48pm UTC](https://www.ruby-forum.com/t/problems-saving-an-uploaded-image-to-an-app-folder/111183 "2007-08-03T16:48:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [August 3, 2007, 4:48pm UTC](https://www.ruby-forum.com/t/problems-saving-an-uploaded-image-to-an-app-folder/111183/1 "2007-08-03T16:48:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [August 3, 2007, 5:02pm UTC](https://www.ruby-forum.com/t/problems-saving-an-uploaded-image-to-an-app-folder/111183/2 "2007-08-03T17:02:22Z")

</div>

You need to create StringIO or TempFile object for storing input\_data

On 8/3/07, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## method, i get an error saying “no ‘write’ method exists for String”: self.content\_type = input\_data.content\_type.chomp else return false end end
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Cheers!

- Pratik  
[http://m.onkey.org](http://m.onkey.org)

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [August 3, 2007, 5:24pm UTC](https://www.ruby-forum.com/t/problems-saving-an-uploaded-image-to-an-app-folder/111183/3 "2007-08-03T17:24:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [August 3, 2007, 5:35pm UTC](https://www.ruby-forum.com/t/problems-saving-an-uploaded-image-to-an-app-folder/111183/4 "2007-08-03T17:35:46Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [August 3, 2007, 5:31pm UTC](https://www.ruby-forum.com/t/problems-saving-an-uploaded-image-to-an-app-folder/111183/5 "2007-08-03T17:31:25Z")

</div>

Umm…try

self.file\_data = ::TempFile.new(input\_data)

On 8/3/07, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## 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
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Cheers!

- Pratik  
[http://m.onkey.org](http://m.onkey.org)

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [August 4, 2007, 6:53pm UTC](https://www.ruby-forum.com/t/problems-saving-an-uploaded-image-to-an-app-folder/111183/6 "2007-08-04T18:53:42Z")

</div>

[http://manuals.rubyonrails.com/read/chapter/56](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

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [August 6, 2007, 10:32am UTC](https://www.ruby-forum.com/t/problems-saving-an-uploaded-image-to-an-app-folder/111183/7 "2007-08-06T10:32:23Z")

</div>

removed\_email\_address@domain.invalid wrote:

> [Peak Obsession](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

That’s great, thanks tyler!
