Upload problem

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

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

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

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
[email protected]

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?

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