Simple File Upload is Failing - String vs File

Hi Everyone,

    Every time I think I'm "getting" RoR something trivial kicks my butt. All I am trying to do is upload text files for processing. I've stripped everything out of my code in order to troubleshoot this and I've hit a wall :frowning: :

Form:
<%= form_tag :action=>"savefile", :multipart=>true%>
<%= file_field (:scanfile, :thefile) %>
<%= submit_tag "upload file" %>
<%= end_form_tag %>

controller:

def savefile
  @thefile = params[:scanfile][:thefile]
  render_text @thefile.content_type
end

The problem is that what I get from the params hash is always just a plain string. It is not a file object with .read or .content_type methods. I get this error in firefox:
undefined method `conent_type' for "scandata.txt":String

Can anyone spot what I have done wrong? I swear that I've used this same simple approach successfully in other apps.

Thanks,

Gary

Hi Gary,

Gary Huntress wrote:

The problem is that what I get from the params hash is
always just a plain string. It is not a file object with
.read or .content_type methods. I get this error in firefox:
undefined method `conent_type' for "scandata.txt":String

There's a real good explanation of the what \ why \ how-to for the problem you're having at http://cleanair.highgroove.com/articles/2006/10/03/mini-file-uploads

hth,
Bill

Thanks Bill, that is much more knowledge than I had 2 hours ago. I
may have a related but I don’t think identical problem.

The issue, and the article cited is that some uploads (typically
“small”) files are treated as StringIO objects and others are TempFile
objects. Note from my error (with the spelling error fixed) that it
appears I’m returned a String not a StringIO object.

undefined method `content_type' for "scandata.txt":String

I did change my little test from .content_type to .read which StringIO
does support and it also failed.

I happen to be developing for the very first time on an OSX and I am
going to try and repeat this on a linux system.

Gary

Bill Walton wrote:

Stand down. I found it, and as usual it was my fault.

The lesson here is that:

<%= form_tag (:action=>"save", :multipart=>"true") %>

is not the same as

<%= form_tag ( {:action=>“save”}, {:multipart=>“true”}) %>

thanks everyone

Gary Huntress wrote: