Ruby on rails 4 如何使用dropzone.js和Rails发送多个文件?
使用Rails 4回形针和SimpleForm。 我正在尝试进行多文件上传。为此,我根据客户要求使用Dropzone.js 这是一个表格,我有:姓名,地址,电话,文件,证书 因此,我为文档和证书创建了两个单独的DropZone 这是我在表格上的代码:Ruby on rails 4 如何使用dropzone.js和Rails发送多个文件?,ruby-on-rails-4,paperclip,simple-form,dropzone.js,Ruby On Rails 4,Paperclip,Simple Form,Dropzone.js,使用Rails 4回形针和SimpleForm。 我正在尝试进行多文件上传。为此,我根据客户要求使用Dropzone.js 这是一个表格,我有:姓名,地址,电话,文件,证书 因此,我为文档和证书创建了两个单独的DropZone 这是我在表格上的代码: = simple_form_for(@user, html: {multipart: true, autocomplete: 'off' }) do |f| = f.input :name, label: false # Column name
= 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
Class用户:index
其他的
work.errors.messages.each do |属性,error|
将error.to_
放置错误
结束
flash[:error]=“在系统中出现眼睛错误。”
将_重定向到:action=>:index
结束
结束
def更新
work=work.find(参数[:id])
if work.update_属性(work_参数)
flash[:notice]=“请在datos进行纠正。”
将_重定向到:action=>:index
其他的
work.errors.messages.each do |属性,error|
flash[:error]=属性“+flash[:error]。to_s+error.to_s+”
结束
#加载新的()
@工作=工作
渲染:编辑,布局:false
结束
结束
def上传文件
放置参数
render:json=>params
结束
私有的
定义工作参数
参数require(:user).permit(:名称,:地址,:文档,:证书)
结束
结束
但这里我有两个问题
1) 当我将文件放入Dropzone时,默认情况下,当您单击“我的提交”按钮并转到“创建”功能时,应上载文件时,调用“上载文件”功能
2) 我用另一个JS上传了多个文件,但是只爬到了最后一个。即:
我在我的文件字段中输入了3个文件:文件1、文件2、文件3。但在DB中,只记录了文件3
有人用回形针上传了多个文件吗?成功使用JS Dropzone.JS或其他
如果是的话,我想和大家分享我的知识