File Upload

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:
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

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

Amir

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

Give attachment_fu by Rick Olson a try - it's very good and allows you
to save to the file system, database, or Amazon S3. There is also
'uploadcolumn'.