我正在尝试使用Paperclip和jquery-fileupload-rails在我的应用程序中上传视频
我按照ulpoad file with paper clip and jquery上传视频,但是正像选择视频一样上传视频,而不必等待按下“提交”按钮,因此我遵循了
1:jQuery file upload: Is it possible to trigger upload with a submit button?选择单个视频并将其上传到“提交”按钮上,而不是在选择文件上。
但是关于将提交按钮放置在表单还是外部表单中存在问题。
当我将提交按钮放在表单中时,它将发送两个请求以按“提交”按钮,一个带有视频,另一个带有不带视频。
而且,如果我在窗体外部放置按钮,则它在按“提交”按钮时会发送一个请求,但在成功创建/更新后,它仍保留在同一页面上。
我想要的只是在按Submit(提交)时,仅发送一个请求,并在成功创建/更新时重定向到索引页面。
在_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[video]')
$('#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()
关于javascript - jQuery文件上传:在data.submit()上发送两个请求,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40179378/