我試圖實現將使用DropZoneJS我的Rails 5應用&拖放功能後不能重定向。我完成了上傳文件,但是當從JavaScript調用控制器時,視圖不會刷新(與正常上載完美配合)。Rails的5:JS調用(DropZoneJS)
的代碼(僅誰的情況下,重要的部分):
index.html.erb
<%= form_for(:printing_file, url: new_printing_file_path, html: {method: :post, class: "dragdropzone"}) do |f| %>
<div>Drag&Drop a file here</div>
<% end %>
<%= form_for(:printing_file, url: new_printing_file_path, html: {method: :post}) do |f| %>
<%= f.file_field :file, class: "printing-file-upload-field", onchange: "this.form.submit()" %>
<% end %>
一是形式將&下降,第二定期上傳(這是重定向好)。
printing_files.coffee
$(document).ready ->
Dropzone.autoDiscover = false
dropzone = new Dropzone('.dragdropzone',
paramName: 'printing_file[file]'
addRemoveLinks: false
headers: { 'Accept': 'application/javascript' })
printing_files_controller.rb
def new
@printing_file = PrintingFile.new(printing_file_params)
@printing_file.user = current_user
@printing_file.name = printing_file_params[:file].original_filename
if @printing_file.save
respond_to do |format|
format.html { redirect_to files_path, notice: "File was correctly uploaded." }
format.js
end
else
redirect_to files_path, alert: "There was an error while uploading file."
end
end
new.js.erb
console.log("This is never printed");
window.location = "<%= files_path() %>";
我也試過在我的printing_files_controller.rb
這裏找到幾個選項,如render js
,但沒有一個可以工作。
輸出:
處理由PrintingFilesController#新如JS
參數:{ 「UTF8」=> 「✓」, 「authenticity_token」=>「PBX2dbP8p + aZZXOkIIw1oFg3QZ02mDo + U + Z02NrBFCE + aNT3gWzwFjmQeGmcBcBItkg6l899iGIxuJljWDl + zA ==「,」printing_file「=> {」file「=>#,@ original_filename =」9.stl「,@ content_type =」application/octet-stream「,@ headers =」Content-Disposition:form -data; name = \「printing_file [file] \」; filename = \「9.stl \」\ r \ nContent-Type:application/octet-stream \ r \ n「>}}
個渲染printing_files/new.js.erb
渲染printing_files/new.js.erb(1.0ms的)
注意,js文件被渲染,但瀏覽器控制檯沒有顯示任何東西。
是一個很好的方法,但重新加載整個頁面。我只想像'redirect_to'那樣重新加載文件視圖。 – fsinisi90