title: jQuery file upload, having file upload as part of a remote form in Rails
body: <p>I have a table with data (name, description, file, etc.) All these are editable inline INDIVIDUALLY via AJAX patch using Rails gem best_in_place and blueimp jQuery file upload. This all works fine.</p>
  
  <p>At the bottom of the table is a form that allows the user to fill out ALL the data and submit a new table row. </p>
  
  <p>I can't get this to submit with the file, unless it somehow submits twice.</p>
  
  <p>form:</p>
  
  <pre><code># has class and ID: "new_item"
  &lt;%= form_for @item, remote: true do |f| %&gt;
  
    &lt;%= f.text_field :description, class: "form-control", placeholder: "description" %&gt;
    &lt;%= f.text_field :url, class: "form-control", placeholder: "www.example.com" %&gt;
  
    # has ID: "item_attachment"
    &lt;%= f.file_field :attachment %&gt;   
  
  
    &lt;%= f.submit "add item", class: "btn btn-success" %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>File upload JS:</p>
  
  <pre><code>  var sendData = true;
  
    $('.new_item #item_attachment').fileupload({
      dataType : 'javascript',
      autoUpload : false,
      add : function(e,data){
        $("form.new_item").on("submit",function(e){
            var $this = $(this);
            e.preventDefault();
            if(sendData){
                data.formData = $this.serializeArray();              
                sendData = false;
                console.log(data);
            }
  
            data.submit();
        });
     },
     done: function(e,data){
         sendData = true;
     }
   });
  </code></pre>
  
  <p>This submits the form twice. Once via the form without the attachment (e.preventDefault(); not working??), and once via jquery upload with the attachment and data, but it is not submitted "as JS" so it doesn't render the .js template to append the new item to the table.</p>
  
  <p>Server logs:</p>
  
  <pre><code>Started POST "/items" for 127.0.0.1 at 2015-05-14 17:35:04 +0700
  Processing by ItemsController#create as */*
    Parameters: {"utf8"=&gt;"✓", "item"=&gt;{"description"=&gt;"TEST", "attachment"=&gt;#
  &lt;ActionDispatch::Http::UploadedFile:0x007fb88d4a9280 @tempfile=#
  ...removed attachment jargon...
  
  Started POST "/items" for 127.0.0.1 at 2015-05-14 17:35:04 +0700
  Processing by ItemsController#create as JS
    Parameters: {"utf8"=&gt;"✓", "item"=&gt;{"description"=&gt;"TEST"...}, "commit"=&gt;"add item"}
  </code></pre>