user1491961
user1491961

Reputation:

Jquery-File-Upload in Rails, Submitting Twice, JSON and HTML?

I have a strange problem-- I have adapted code from the Jquery-File-Upload demo example into my app, and whenever I upload an image, it seems like the upload tries to post twice, once with JSON, and then once with HTML.

The JSON Post is working correctly, but the HTML Post is posting null parameters, which triggers an error.

I am confused, because I am literally copying the EXACT same files as the demo, out of box. I added the 'Uploads' model, controller, views and routes to my app, and I'm literally just running the example from localhost:3000/uploads as a sanity test, and it's not working. I have not changed any of the code from the demo, so I don't understand why its suddenly firing twice now.

Started POST "/uploads" for 127.0.0.1 at 2013-11-19 03:21:39 -0500
Processing by UploadsController#create as JSON
Parameters: {"utf8"=>"✓",   "authenticity_token"=>"SLkjXV0R/pEneunEUzHLOGJ3iKm4KLJgHrAudzWs5v4=", 
"upload"=>{"upload"=>#<ActionDispatch::Http::UploadedFile:0x007fbef632acf0 
@original_filename="aliceten.png", @content_type="image/png", @headers="Content-
Disposition: form-data; name=\"upload[upload]\"; filename=\"aliceten.png\"\r\nContent-
Type: image/png\r\n", @tempfile=#
<File:/var/folders/sb/1k3xvfvn63d8p11mxry4zh6c0000gq/T/RackMultipart20131119-42740-vi3x5y>>}}
(0.1ms)  BEGIN
 SQL (0.3ms)  INSERT INTO `uploads` (`created_at`, `updated_at`, `upload_content_type`,
 `upload_file_name`, `upload_file_size`, `upload_updated_at`) VALUES ('2013-11-19 08:21:39', '2013-11-19 08:21:39', 'image/png', 'aliceten.png', 5947, '2013-11-19 08:21:39')
[paperclip] Saving attachments.
[paperclip] saving /uploads/uploads/000/000/027/original/aliceten.png
[AWS S3 200 0.79522 0 retries]    
put_object(:acl=>:public_read,:bucket_name=>"elasticbeanstalk-us-west-2-
859932007099",
:content_length=>5947,:content_type=>"image/png",:data=>Paperclip::UploadedFileAdapter: aliceten.png,:key=>"uploads/uploads/000/000/027/original/aliceten.png")  

(0.4ms)  COMMIT
Completed 201 Created in 803ms (Views: 0.8ms | ActiveRecord: 0.7ms)


Started POST "/uploads" for 127.0.0.1 at 2013-11-19 03:21:40 -0500
Processing by UploadsController#create as HTML
Parameters: {"utf8"=>"✓", 
"authenticity_token"=>"SLkjXV0R/pEneunEUzHLOGJ3iKm4KLJgHrAudzWs5v4="}
(0.1ms)  BEGIN
SQL (0.2ms)  INSERT INTO `uploads` (`created_at`, `updated_at`, `upload_content_type`, 
`upload_file_name`, `upload_file_size`, `upload_updated_at`) VALUES ('2013-11-19 
08:21:40', '2013-11-19 08:21:40', NULL, NULL, NULL, NULL)
[paperclip] Saving attachments.
(0.4ms)  COMMIT
Completed 200 OK in 3ms (Views: 0.1ms | ActiveRecord: 0.6ms)

In the Uploads#create function, the respond with does have layout set to false. Not sure why its trying so hard to serve up HTML?

if @upload.save
    format.html {
      render :json => [@upload.to_jq_upload].to_json,
      :content_type => 'text/html',
      :layout => false
    }
    format.json { render json: {files: [@upload.to_jq_upload]}, status: :created, location: @upload }
  else

Upvotes: 0

Views: 1078

Answers (2)

PeteW
PeteW

Reputation: 731

I had exactly this problem and found that I was initialising jQuery File Upload using a class selector:

$('.image-upload').fileupload({...})

And I had that class applied on a div and a form element - so that was what was firing it twice.

Upvotes: 0

Sabyasachi Ghosh
Sabyasachi Ghosh

Reputation: 2785

Seems like you have give the :remote => true and :multipart => true option in the form remove the :remote => true option and try to submit the from using jQuery and make sure you need to disable the html form submit on clicking the submit button.and also need to check the there only 1 file upload method present in your .js file and make sure one file is not loaded multiple time.

Upvotes: 5

Related Questions