JangoSteve / remotipart

Rails jQuery file uploads via standard Rails "remote: true" forms.
http://os.alfajango.com/remotipart
Other
1k stars 215 forks source link

Remotipart posting twice #129

Open lukasburns opened 9 years ago

lukasburns commented 9 years ago

Hi, im using remotipart and paperclip for the file and it is posting twice and creating 2 objects, once with a html post and the second one with the JS post(the one I want). im using rails 4.2.1 and the lates version of remotipart. Hope someone could help me Thanx

monttda commented 9 years ago

Same Issue with Rails '4.2.1' and remotipart 1.2.1. it does

Started POST "/operations/import" for 127.0.0.1 at 2015-06-10 18:03:50 -0430 Processing by OperationsController#import as / Parameters: {"object Object"=>{","=>{"object Object"=>{","=>{"object Object"=>nil}}}}}

...

Started POST "/operations/import" for 127.0.0.1 at 2015-06-10 18:03:50 -0430 Processing by OperationsController#import as JS Parameters: {"utf8"=>"✓", "commit"=>"Import", "remotipartsubmitted"=>"true", "X-Requested-With"=>"IFrame", "X-Http-Accept"=>"text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, /_; q=0.01", "file"=>#<ActionDispatch::Http::UploadedFile:0x007fa88d514428 @tempfile=#<Tempfile:/tmp/RackMultipart20150610-5975-ldl9d9.csv>, @original_filename="test.csv", @content_type="text/csv", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"test.csv\"\r\nContent-Type: text/csv\r\n">}

...

This happens when uploading a file and using this form for doing so (I'm using HAML so the reason for the syntax)

= form_tag import_operations_path,  remote: true, method: :post, multipart: true , id: "csv_document"do
    .row
      .col-xs-4
        = file_field_tag :file 
      .col-xs-4
        = submit_tag "Import", id: "send_csv"
      .col-xs-4 

Also one of the resquests has params without the uploaded file (the one as /) while the other has the file as You can see in the code up

As a solution to avoid this double call while the error persist I used the the next coffeescript which prevents the empty request(the one without the file ) to fire and only sends the one with the file

    $(document).ready ->
      $('#send_csv').on 'click', (e) ->
        e.preventDefault()
        $('#csv_document').submit()
ThomasSertorio commented 7 years ago

Same thing here with rails 5 and remotipart 1.3.1. I've been checking other issues related to "double submit" but none of them help me to resolve the problem.

Thanks @monttda for the temporary workaround.

Ammet commented 7 years ago

Catching Uncaught TypeError: $.rails.ajax(...).complete is not a function too..

grsahil20 commented 6 years ago

Is this still open issue, facing the same problem?

capjuancode commented 6 years ago

<%= semantic_form_for([blah, blah], remote: true ) do |f| %>

is like jquery-ujs is not preventing its default functionality.

togino77 commented 6 years ago

jqXHR.complete() callback was removed from jQuery 3.0. Using jqXHR.always() instead of jqXHR.complete() gets rid of 'Uncaught TypeError: $.rails.ajax(...).complete is not a function' error. So 'ajax:beforeSend.remotipart' event function returns 'false' properly, browser never fire default submit event (second request as HTML).