views:

318

answers:

2

Hi, I have the below form in my view:

<% form_for(@filedata, :url=>{ :controller=>"filedatas", :action=>"create"}, :multipart => true) do |f| %>

  <label>Select File</label> : <%#= f.file_field :file %>
<%= file_field_tag 'uploadedFile' %>
  <%= f.submit 'Upload' %>
</p>

<% end %>

I've commented out the f.file_field but I have tested on both and both give me the same problem. They just return the name of the file and I get a string. methods like .path and .original_filename cause an error.

In my list of parameters I get: "uploadedFile"=>"test"

(the name of my file is test.txt)

Anyone have any suggestions?

A: 

This is happening because the contents of the file aren't stored in the attribute like other fields. File contents are stored in the POST data, and have to be retrieved. Extra steps are required.

This article explains one way to do it manually. But a better approach is to use a plugin like thoughtbot's paperclip to handle this for you.

Jaime Bellmyer
Thanks, I was actually working from that article (which does not specify :html). Looks like I need to get moving on paperclip thought!
TravisKs
A: 

Sorry, I may have misunderstood your original question. It looks like you have an error in your form_for call. It should be:

<% form_for(@filedata, :url=>{ :controller=>"filedatas", :action=>"create"}, :html => {:multipart => true}) do |f| %>

You were missing the ":html =>{}" part. Also, you can shorten it down like this:

<%= form_for @filedata, :html => {:multipart => true} do |f| %>
Jaime Bellmyer
Thanks! the :html declaration was what was missing. unfortunately I did need the :url symbol to tell rails where to go. You're shortened version is what it originally looked like and made problems.
TravisKs