如何使用带有Rails的dropzone.js发送多个文件?

时间:2014-04-23 06:40:34

标签: ruby-on-rails-4 paperclip simple-form dropzone.js

使用Rails 4 Paperclip和SimpleForm。 我正在尝试上传多个文件。为此我根据客户要求使用Dropzone.js。

这是我的表格:姓名,地址,电话,文件,证书。

所以我为文档和证书创建了2个单独的dropzone。

这是我在表单上的代码:

= simple_form_for(@user, html: {multipart: true, autocomplete: 'off' }) do |f|
  = f.input :name, label: false # Column name in table User
  = f.input :address, label: false # Column address in table User
  #attachments-documents.dropzone # Column document in table User
  #attachments-certificates.dropzone # Column certificate in table User

:javascript
  var attachments_1 = new Dropzone("div#attachments-documents", { url: "#{upload_file_biddings_path}"});
  var attachments_2 = new Dropzone("div#attachments-certificates", { url: "#{upload_file_path}"});
  Dropzone.options.attachmentsDocuments = {
    paramName: 'user[document]',
    maxFilesize: 20,
    parallelUploads: 3,
    addRemoveLinks : true,
    autoProcessQueue: false
  }

  Dropzone.options.attachmentsCertificates = {
    paramName: 'user[certificate]',
    maxFilesize: 20,
    parallelUploads: 3,
    addRemoveLinks : true,
    autoProcessQueue: false
  }

控制器:

Class User < ApplicationController
  [...]
  def create
    work = Work.new(work_params)
    if work.save
      flash[:notice] = "Se ha creado correctamente la Obra."
      redirect_to :action => :index
    else
      work.errors.messages.each do |attribute, error|
          puts error.to_s
          puts error
      end
      flash[:error] =  "Ha ocurrido un error en el sistema."
      redirect_to :action => :index
    end
  end

  def update
    work = Work.find(params[:id])
    if work.update_attributes(work_params)
      flash[:notice] = "Se ha actualizado correctamente los datos."
      redirect_to :action => :index
    else
      work.errors.messages.each do |attribute, error|
        flash[:error] =  attribute " " + flash[:error].to_s + error.to_s + "  "
      end
      # Load new()
      @work = work
      render :edit, layout: false
    end
  end

  def upload_file
    puts params
    render :json => params
  end

  private
  def work_params
    params.require(:user).permit(:name, :address, :document, :certificate)
  end
end

但在这里我有两个问题。

1)当我将文件放入Dropzone时,默认情况下,当您单击我的提交按钮并转到创建功能时,应该上传文件时调用“upload_file”函数。

2)我做了一个测试,用另一个JS上传多个文件,但只设法爬上最后一个。即:

我输入了我的file_field 3文件:file1,file2,file3。但是在DB中,只记录了文件3。

是否有人使用Paperclip上传了多个文件?使用JS Dropzone.js还是其他成功?

如果是这样,我想分享知识。

0 个答案:

没有答案