dropzone.js在失败时不会正确地重定向或呈现错误

时间:2016-08-15 18:06:51

标签: ruby-on-rails dropzone.js

dropzone.js一切正常,除非照片无法上传。

如果提交的表单附带的照片太大或者没有照片而提交的表单会发生以下情况:

在Heroku控制台

  

开始发布" /照片" for ...由PhotosController处理#create as HTML   已完成422 Unprocessable Entity ... method = POST path =" / photos

然后浏览器显示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'
});

结果:

  

开始发布" /照片"由PhotosController处理#create as HTML   ...在422ms内完成了422个不可处理的实体(ActiveRecord:14.5ms)   ... method = GET path =" / photos / new" ... method = POST path =" / photos"

然后我们回到空白的example.com/photos

1 个答案:

答案 0 :(得分:0)

redirect_to new_photos_path 

应该是 - >

redirect_to new_photo_path