# Upload problem

**URL:** <https://www.ruby-forum.com/t/upload-problem/179799>\
**Category:** Rails\
**Created:** [December 4, 2009, 1:03am UTC](https://www.ruby-forum.com/t/upload-problem/179799 "2009-12-04T01:03:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [December 4, 2009, 1:03am UTC](https://www.ruby-forum.com/t/upload-problem/179799/1 "2009-12-04T01:03:49Z")

</div>

Hey guys,  
I’m trying to make a really simple uploader and I’m having trouble…  
I’m trying to follow this tutorial:

> **[Ruby on Rails - File Uploading](http://www.tutorialspoint.com/ruby-on-rails/rails-file-uploading.htm)**
>
> Ruby on Rails File Uploading - You may have a requirement in which you want your site visitors to upload a file on your server. Rails makes it very easy to handle this requirement. Now we will proceed with a simple and small Rails project.

the view:

\<% form\_for :profile, :url =\> { :controller =\> ‘profile’, :action =\>  
‘update’ }, :multipart =\> true do %\>  
\<%= file\_field ‘avatar’, ‘datafile’ %\>  
upload to profile  
\<% end %\>

the controller:

post = DataFile.save(params[:avatar])

the model:

class DataFile \< ActiveRecord::Base  
def self.save(upload)  
name = File.basename(upload[‘datafile’])  
directory = “public/avatars”  
# create the file path  
path = File.join(directory, name)  
# write the file  
File.open(path, “wb”) do |file|  
file.puts upload[‘datafile’].read  
end  
end  
end

the problem is that for some reason the file (test.jpg) is coming in  
only as a string:  
undefined method `read’ for “test.jpg”:String

Can anybody help me out?? I’m kinda pressed for time…

Thanks,

- Jeff

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [December 4, 2009, 2:37am UTC](https://www.ruby-forum.com/t/upload-problem/179799/2 "2009-12-04T02:37:42Z")

</div>

Jeff M. wrote:

> Hey guys,  
> I’m trying to make a really simple uploader and I’m having trouble…  
> I’m trying to follow this tutorial:  
> [Ruby on Rails - File Uploading](http://www.tutorialspoint.com/ruby-on-rails/rails-file-uploading.htm)
> 
> the view:
> 
> \<% form\_for :profile, :url =\> { :controller =\> ‘profile’, :action =\>  
> ‘update’ }, :multipart =\> true do %\>  
> \<%= file\_field ‘avatar’, ‘datafile’ %\>  
> upload to profile  
> \<% end %\>
> 
> the controller:
> 
> post = DataFile.save(params[:avatar])
> 
> the model:
> 
> class DataFile \< ActiveRecord::Base  
> def self.save(upload)  
> name = File.basename(upload[‘datafile’])  
> directory = “public/avatars”  
> # create the file path  
> path = File.join(directory, name)  
> # write the file  
> File.open(path, “wb”) do |file|  
> file.puts upload[‘datafile’].read  
> end  
> end  
> end
> 
> the problem is that for some reason the file (test.jpg) is coming in  
> only as a string:  
> undefined method `read’ for “test.jpg”:String
> 
> Can anybody help me out?? I’m kinda pressed for time…

Why not just use Paperclip?

> Thanks,
> 
> - Jeff

Best,  
–Â  
MarnenÂ Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [December 6, 2009, 12:42am UTC](https://www.ruby-forum.com/t/upload-problem/179799/3 "2009-12-06T00:42:47Z")

</div>

Well, I’d like to just get it done this way if I can… I’ve got this  
now:

def uploadAvatar(file)  
name = file.original\_filename  
directory = “public/avatars”  
# create the file path  
path = File.join(directory, name)  
# write the file  
File.open(path, “wb”) do |file|  
file.puts file.read  
end  
end

which gives me the error “not opened for reading”. Any suggestions guys?

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [December 6, 2009, 1:00am UTC](https://www.ruby-forum.com/t/upload-problem/179799/4 "2009-12-06T01:00:01Z")

</div>

nevermind, I’m stupid… I got it

def uploadAvatar(upload)  
name = upload.original\_filename  
directory = “public/avatars”  
# create the file path  
path = File.join(directory, name)  
# write the file  
File.open(path, “wb+”) do |file|  
file.write(upload.read)  
end  
end
