I am trying to implement the uploading of a file to a remote server
I get error while trying to write the file on the server. The error I
get is the following:
undefined method `rewind’ for #String:0x2aaaad062eb8
It seems to be treating my file as a string instead of a File object.
--------- Code is below ------------------
VIEW:
<%= start_form_tag(:action => “save_image”, :multipart => true) %>
…
Image <%= file_field("image", "picture", "size"=>"30") %> <%= submit_tag(" SUBMIT ") %>…
<%= end_form_tag %>
CONTROLLER:
def save_image
@image = Image.new(params[:image])
# attempt to save entry in the db
if @image.save
# info saved in db OK. Save file on the server
write_file(params[:image])
redirect_to(:controller => 'shto', :action =>
‘index’)
else
render(:action => :get)
end
end
def write_file(uploaded_file)
uploaded_file['picture'].rewind
File.open(“pictures/upload/#{@image.id}.#{@image.extention}”, “wb”) {
|f| f.write(uploaded_file[‘picture’].read) }
end
end
MODEL:
class Image < ActiveRecord::Base
def picture=(image_field)
extname_dot = File.extname(image_field)
self.extention = extname_dot.slice(1,
extname_dot.length)
self.submitDate = Time.now
end
end
I tried implementing this with Agile web development rails book, and
also tried using other examples on the web, it always comes down to this
issue. Perhaps I am missing something, or this has to do with my ruby
version or the fact that I’m running this on Apache…
Any help would be greatly appreciated.
. . . . . . . . . . . . . . . . . . .
Eduard V. Kotysh
Appistry | The Fabric of Business
[email protected] mailto:[email protected]
direct. 314 336 2859 cell. 314 518 0408