[Rails] file object treated as a string

Ed Kotysh
Tue, 15 Aug 2006 14:10:01 -0000

I am trying to implement the uploading of a file to a remote server

My upload form looks like this:

<%= start_form_tag(:action => "save_image", :multipart =>
true) %>

Description
<%= text_area("image", "descript",
"cols" => 33, "rows" => 5) %>

Image
<%= file_field("image", "picture",
"size"=>"30") %>

<%= submit_tag(" SUBMIT ") %>

<%= end_form_tag %>


This links to "save_image" action in the controller. Controller looks
like this:

        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


My image model looks like this:

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


When I click the submit button on the form, it saves the image entry in the
database OK, but errors while trying to write the file to the server. The error
I get is the following:

undefined method `rewind'' for #<String:0x2aaaad062eb8>

It seems to be treating uploaded_file[''picture''] as a string
instead of a File object. 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.

Thanks!

...Ed
-------------- next part --------------
An HTML attachment was scrubbed...