javascriptruby-on-railscoffeescriptdropzone.jsdropzone

Dropzone Rails Gem How to Send with 0 Files


I want to know the right way on how I handle still sending even when I have 0 files uploaded. I have a form that has a lot of data to input and the images are optional.

I could probably send this with an entirely separate Ajax request that is a duplicate of 'sendingmultiple', otherwise I am unsure if this is possible within the confines of dropzones solutions in handling this case.

  Dropzone.options.dropzone =
    url: '/test/create'
    autoProcessQueue: false
    clickable: true
    uploadMultiple: true
    timeout: 180000
    parallelUploads: 8
    maxFiles: 8
    maxFilesize: 5
    acceptedFiles: 'image/*'
    addRemoveLinks: true
    headers: 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
    init: ->
      dzClosure = this

      document.getElementById('submit-button').addEventListener 'click', (e) ->
        e.preventDefault()
        e.stopPropagation()
        dzClosure.processQueue()
        return

      @on 'sendingmultiple', (data, xhr, formData) ->
        formData.append 'item_number', jQuery('#item_number').val()
        ... more form data
        return
      @on 'success', (file, responseText) ->
        window.location.href = url
        return
      return

Solution

  • Okay I have found a solution that I think works the best. Anyone else with something better please provide.

      document.getElementById('submit-button').addEventListener 'click', (e) ->
        uploaded_files = $('.dz-preview').length
        if uploaded_files > 0
          e.preventDefault()
          e.stopPropagation()
          dzClosure.processQueue()
          return
    

    I just changed it so we only override the button to this function if we detect any image previews, which works for my situation. So we still have the normal create/update on the controller as normal that fires when no images are uploaded. Then I have an image_upload_create function in the controller that updates or creates depending if it has a id in the forms.

    The solution is all in coffeescript for those confused about syntax.