# File Upload

**URL:** <https://www.ruby-forum.com/t/file-upload/108869>\
**Category:** Rails\
**Created:** [July 8, 2007, 12:51pm UTC](https://www.ruby-forum.com/t/file-upload/108869 "2007-07-08T12:51:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fernando](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/779978/32.png) [@fernando](https://www.ruby-forum.com/u/fernando)\
**Post date:** [July 8, 2007, 12:51pm UTC](https://www.ruby-forum.com/t/file-upload/108869/1 "2007-07-08T12:51:19Z")

</div>

Hi,

I’m still quite new to RoR and I am having trouble uploading files to  
a file system. I have been following this tutorial:  
[Peak Obsession](http://manuals.rubyonrails.com/read/chapter/77) :  
1.2.1. The simplest thing that could possibly work

But when I try to save a file I get “You have a nil object when you  
didn’t expect it!  
The error occurred while evaluating nil.read”  
In this very simple example its the controller that does all the work:

def save  
File.open(“#{RAILS\_ROOT}/public/images/testWorked.jpg”, “wb”) do |  
f|  
f.write(@params[‘picture\_file’].read)  
end

Why is my @params[‘picture\_file’] object empty?

Any assistance would be much appreciated.

Tim

---

<div class="post-metadata">

**Author:** ![fernando](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/779978/32.png) [@fernando](https://www.ruby-forum.com/u/fernando)\
**Post date:** [July 8, 2007, 2:51pm UTC](https://www.ruby-forum.com/t/file-upload/108869/2 "2007-07-08T14:51:31Z")

</div>

Hi,

If these images are coming from a browser, they need to have multi-  
part encoding. Then, you’ll need Ruby code to decode this.

Why don’t you use some complete plugin such as acts\_as\_attachment or  
attachment\_fu?  
[http://weblog.techno-weenie.net/articles/acts\_as\_attachment](http://weblog.techno-weenie.net/articles/acts_as_attachment)

Amir

---

<div class="post-metadata">

**Author:** ![fernando](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/779978/32.png) [@fernando](https://www.ruby-forum.com/u/fernando)\
**Post date:** [July 8, 2007, 3:17pm UTC](https://www.ruby-forum.com/t/file-upload/108869/3 "2007-07-08T15:17:33Z")

</div>

Amir,

Thanks for the reply,

I want to save the files to the file system and not to a database. fto  
set the enctype I used multipart =\> true and the html output is set to  
enctype=“multipart/form-data” so that should be fine.  
Is the ruby code:

File.open("#{RAILS\_ROOT}/public/images/testWorked.jpg", “wb”) do |  
f|  
f.write(@params[‘picture\_file’].read)

incorrect for dealing with file uploads?

I’ll see if i can find a plugin that allows saves to the file system.

Tim

---

<div class="post-metadata">

**Author:** ![fernando](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/779978/32.png) [@fernando](https://www.ruby-forum.com/u/fernando)\
**Post date:** [July 8, 2007, 4:00pm UTC](https://www.ruby-forum.com/t/file-upload/108869/4 "2007-07-08T16:00:39Z")

</div>

Give attachment\_fu by Rick O. a try - it’s very good and allows you  
to save to the file system, database, or Amazon S3. There is also  
‘uploadcolumn’.
