Roaring Stones
Roaring Stones

Reputation: 1054

Html form does not include in post's parameters file data

For reasons I can't even guess about, my rails app does not work any more. I've not changed anything in the rails code so I suppose it may be something about browser.

Simple form, generated by Simple_form:

<%= simple_form_for @order, :url => fillparameters_order_path(@order), :remote => true do |f| %>

  <%= f.association :aspectRatio,  collection: @order.project.aspect_ratios %>
  <%= f.input :client_email, :as => :string %>
  <%= f.input :soundtrack, :as => :file %>
  <%= f.input :project_id, :as => :hidden %>
  <%= f.button :submit %>
<% end %>

It generates the following:

form id="edit_order_4" class="simple_form edit_order" novalidate="novalidate" method="post" enctype="multipart/form-data" data-remote="true" action="/orders/4/fillparameters" accept-charset="UTF-8"
    <input id="order_soundtrack" class="file optional" type="file" name="order[soundtrack]">
    <input class="btn" type="submit" value="Send!" name="commit">

Besides other inputs of course.

But after hitting the commit button, the Firebug console and webrick log show that all that is sent is anything except my file-field:

Started PUT "/orders/4/fillparameters" for 127.0.0.1 at 2013-08-04 21:00:03 +0900
Processing by OrdersController#fillparameters as JS
Parameters: {
    "utf8" => "✓",
    "authenticity_token" => "VpddWLzi7Czzl0L+gbd5wVfjbJ1pQnfI53L86BwbH/E=",
    "order" => {
        "aspect_ratio_id" => "1",
        "client_email" => "****@gmail.com",
        "project_id" => "1"
    },
    "commit" => "Send!",
    "id" => "4"
}

Any ideas, guys?

Upvotes: 2

Views: 690

Answers (1)

zuozuo
zuozuo

Reputation: 386

The fact is that: you can't upload file with :remote => true option in rails. There is two ways allow you use AJAX for file uploading:

  1. The rails way: using gem remotepart
  2. Jquery way: using a Jquery file upload plugin, there is a lot of such plugin, such as https://github.com/blueimp/jQuery-File-Upload

    http://malsup.com/jquery/form/ and so on.

Upvotes: 1

Related Questions