2016-08-15 27 views
0

除非照片無法上傳,否則一切工作都正常,dropzone.jsdropzone.js在重定向失敗時不會正確重定向或呈現錯誤

如果窗體與照片提交

連接是太大或形式中沒有照片,將出現以下情況提出:

在Heroku的控制檯

開始POST「/照片」的。通過PhotosController#..Processing創建爲HTML 完成422無法處理的實體...方法= POST路徑=「/照片

然後,瀏覽器顯示example.com/photos但屏幕是空白。

$(document).ready(function() { 
    var dropzone; 
    Dropzone.autoDiscover = false; 
    dropzone = new Dropzone('#dropform', { 
    maxFiles: 1, 
    maxFilesize: 1, 
    paramName: 'photo[picture]', 
    headers: { 
     "X-CSRF-Token": $('meta[name="csrf-token"]').attr('content') 
    }, 
    addRemoveLinks: true, 
    clickable: '#image-preview', 
    previewsContainer: '#image-preview', 
    thumbnailWidth: 200, 
    thumbnailHeight: 200, 
    parallelUploads: 100, 
    autoProcessQueue: false, 
    uploadMultiple: false 
    }); 
    $('#item-submit').click(function(e) { 
    e.preventDefault(); 
    e.stopPropagation(); 
    if (dropzone.getQueuedFiles().length > 0) { 
     return dropzone.processQueue(); 
    } 
    else { 
     return $('#dropform').submit(); 
    } 

    }); 
    return dropzone.on('success', function(file, responseText) { 
    return window.location.href = '/photos/' + responseText.id; 
    }); 
    return dropzone.on('error', function(file, errorMessage, xhr) { 
    console.log('error'); 
    }); 
}); 

PhotosController

def create 
    @photo = current_user.photos.build(photo_params) 
    respond_to do |format| 
     if @photo.save! 
     format.html { redirect_to @photo, notice: 'Photo was successfully created.' } 
     format.json { render json: @photo } 
     else 
     format.html { redirect_to new_photos_path, notice: 'Photo was not created'} 
     format.json { redirect_to photos_path and return @photo.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

編輯

return dropzone.on('error', function(file, errorMessage, xhr) { 
    console.log('error'); 
    window.location.href = '/photos/new' 
}); 

結果:

開始POST「/照片「處理由PhotosController#創建爲HTML ...已完成422在422毫秒內不可處理的實體(ActiveRecord:14.5ms) ... method = GET path =」/ photos/new「... method = POST path =」/ photos 「

,然後我們又回到了空白example.com/photos

回答

0
redirect_to new_photos_path 

應該是 - >

redirect_to new_photo_path 
+0

喜鄧肯;您的代碼專用答案在某些情況下會更有用。例如,您可以解釋爲什麼您認爲需要更改代碼;以及爲什麼它會解決提問者的問題。 –