JQuery文件上传:在data.submit()上发送两个请求

我正在尝试使用Paperclipjquery-fileupload-rails在我的应用中上传video

我跟着ulpoad文件用纸夹和jquery上传video,但是在我选择video时上传video而不等待按下提交按钮,所以我跟着1 : jQuery文件上传:是否有可能触发上传提交按钮? 选择单个video并将其上传到提交按钮,而不是选择文件。

但是我是否在表单或外部表单中放置提交按钮存在问题。

当我在表单中提交提交按钮时,它发送两个请求按下提交按钮,一个带有video,第二个没有video。

如果我把按钮放在窗体之外,它按下提交按钮发送一个请求但是在成功创建/更新之后它仍然在同一页面上。

我想要的只是按下提交,只发送一个请求并在成功创建/更新时重定向到索引页面。

在_form.slim中

= simple_form_for [:admin,add] do |f| .wizard-content .row .col-sm-6 = f.input :video, multiple: false, wrapper: :horizontal_file_input button#submit_button.btn.btn-primary | Save 

在adds.coffee中

  jQuery -> $('#add_video').attr('name','add') $('#add_video').fileupload $('#submit_button').off('click').on 'click', -> data.submit() 

在控制器我有

 def create if add.save redirect_to admin_adds_path flash[:success]= "Add Created" else render :new end end def update if add.update(add_params) redirect_to admin_adds_path flash[:success]= "Add Updated" else render :edit end end 

还有一个_add.slim

 = image_tag(@add.uploaded_file(:small), class: 'thumb') 

我也创建了create.js.erb和edit.js.erb作为我遵循的建议教程(尽管我不明白它的用法)

 - if @add.new_record? | alert('Failed'); - else | if ($('h1') !== undefined) { $('h1').append(" =j render partial: 'adds/add', locals: { add: @add } | "); } 

这有什么不对吗?

尝试使用表单内的按钮并使用event.preventDefault(),因为表单似乎正在执行默认操作,即发送数据并重新加载页面并调用js。

 $('#submit_button').off('click').on 'click', (event) -> event.preventDefault() data.submit() 

当您将按钮放在窗体外时,您可以处理done事件和重定向。 请记住,此POST是ajax,如果您发布更改或新元素,则通过ajax no通过Web浏览器页面获得响应。

有些人喜欢

 $('#fileupload').fileupload({ dataType: 'json', add: function (e, data) { $("#up_btn").off('click').on('click', function () { data.submit(); }); done: function (e, data) { window.location.href = "data.next_url"; } }, }); 

也许你需要响应一些json(而不是重定向redirect_to admin_adds_path )从控制器传递到javascript信息(有些像successerrornext_urlothers