Rails中的Jquery-File-Upload,提交两次,JSON和HTML?

时间:2013-11-19 08:39:30

标签: ruby-on-rails json jquery

我有一个奇怪的问题 - 我已将Jquery-File-Upload演示示例中的代码调整到我的应用程序中,每当我上传图像时,似乎上传尝试发布两次,一次使用JSON,然后一次使用HTML。

JSON Post工作正常,但HTML Post发布了空参数,这会触发错误。

我很困惑,因为我实际上正在复制与演示完全相同的文件,开箱即用。我将“上传”模型,控制器,视图和路由添加到我的应用程序中,我实际上只是运行localhost:3000 / uploads中的示例作为完整性测试,但它无法正常工作。我没有更改演示中的任何代码,所以我不明白为什么它现在突然发射两次。

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)

在Uploads #create功能中,response with的布局设置为false。不知道为什么它如此努力地提供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

2 个答案:

答案 0 :(得分:5)

好像你给了:remote =&gt; true :multipart =&gt;表单中的true 选项删除:remote =&gt;真正的选项,并尝试使用jQuery提交,并确保您需要在单击提交按钮时禁用html表单提交。还需要检查中仅存在1个文件上载方法。 js 文件并确保多次加载一个文件。

答案 1 :(得分:0)

我遇到了这个问题,发现我正在使用类选择器初始化jQuery文件上传:

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

我将这个类应用于div 一个表单元素 - 所以这就是两次触发它。