使用Dropzone上传图像时的ActionController :: UnknownFormat

时间:2018-10-21 15:48:08

标签: ruby-on-rails ruby-on-rails-5

关于我的Dropzone JS代码段,我有一个新错误。我使用dropzone从#Edit视图上传图像。

一个图像是由另一个控制器创建动作创建的,我重定向到该非常编辑动作。

这是我收到此新错误的地方:

Completed 406 Not Acceptable in 14ms (ActiveRecord: 1.1ms)



ActionController::UnknownFormat (PhotographesController#edit is missing a template for this request format and variant.

request.formats: ["application/json"]
request.variant: []):

actionpack (5.2.0) lib/action_controller/metal/implicit_render.rb:42:in `default_render'
actionpack (5.2.0) lib/action_controller/metal/basic_implicit_render.rb:6:in `block in send_action'
actionpack (5.2.0) lib/action_controller/metal/basic_implicit_render.rb:6:in `tap'
actionpack (5.2.0) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
actionpack (5.2.0) lib/abstract_controller/base.rb:194:in `process_action'
.....

过去曾经完美地运行过。但是,由于我已将此代码段设置到位,所以我做了很多事情:安装gem "serviceworker-rails"并推迟javascript ..

我没有延迟Javascript,但是错误仍然存​​在。它与serviceworker gem有关吗?

编辑

Photographes#edit视图(内联)中的Dropzone代码段

<script>

  // Dropzone = dynamic



  var AUTH_TOKEN=$('meta[name="csrf-token"]').attr('content');
    Dropzone.autoDiscover = false;
    var myDropzone = new Dropzone("div#mydropzone",{
        url: "<%= photographe_photographephotos_path(@photographe.hashed_id) %>",
        autoProcessQueue: false,
        autoDiscover: false,
        uploadMultiple: true,
        addRemoveLinks: true,
        // clickable: false,
        parallelUploads: 12,
        maxFilesize: 5,
        maxFiles: 12,
        acceptedFiles: 'image/jpg, image/jpeg, image/png',
        params:{
            'authenticity_token':  AUTH_TOKEN
        },
        successmultiple: function(data,response){
            $('#msgBoard').append(response.message).addClass("alert alert-success");
            $('#msgBoard').delay(2000).fadeOut();
            $('#fileslist').val(response.filesList);
            $('#photographedit').off('submit').submit();
        }
    });
    $('#photographedit').submit(function(e){
        if(myDropzone.getQueuedFiles().length > 0){
            e.preventDefault();
            myDropzone.processQueue();
        }
    });

</script>

然后拍摄Photographephotos#create

def create
    @photographe = Photographe.find_by(hashed_id: params[:photographe_hashed_id])

    if params[:file].present?
      uploaded_pics = params[:file]
      maximum_images=12
      available_images = maximum_images - @photographe.photographephotos.count

      n_keys = uploaded_pics.keys.first(available_images)
      filtered_pics = uploaded_pics.slice(*n_keys)

      filtered_pics.each do |index,pic|
         @image = @photographe.photographephotos.new
         @image.image = pic
         @image.image_file_name = "Copyright" + @photographe.professionnel.first_name.to_s  + @photographe.professionnel.last_name.to_s + ".JPG"
         @image.save
      end
    end
    redirect_to edit_photographe_path(@photographe.hashed_id)
  end

最后的重定向返回(应该返回)到Photographes#edit视图。如果以前能正常工作的话,有什么好笑的呢?

我看到人们以前对Jbuilder的问题和github一样。我更新了Jbuilder宝石,但是没有成功。

此外,图片paperclip也已正确更新。当映像创建结束时重定向失败时,我会从日志中收到此错误。

1 个答案:

答案 0 :(得分:1)

似乎控制器正在尝试以不存在的格式呈现模板,也许您可​​以尝试像这样显式指定格式:

class UploadService constructor() : BaseTaskService() {

    private lateinit var filePaths: ArrayList<String>
    private var sourceID: Int = 0
    private var typeID: Int = 0

...
onStartCommand ..
onBind ..
}

如果您收到了一种特定格式的请求,并想以另一种格式呈现模板,则可能会发生这种情况。

在您的情况下,我不确定:format选项是否应放在_path()或redirect_to()方法内。如果上述方法无效,请尝试以下操作:

redirect_to edit_photographe_path(@photographe.hashed_id), :format => :html