如何使用 jquery-fileupload 处理嵌套附件



我正在使用jquery-fileupload-rails进行多个文件上传。

我想实现设置文档名称并向其添加多个附件的能力。

但是现在,当我选择 3 个附件时,它会创建 3 个documents每个附件都有一个附件。

我想我需要以某种方式更改添加附件的形式。我添加了多个选项和编码名称。

我想使用这个插件,因为稍后我会想要添加拖放功能。

= simple_form_for [:member, @document], html: { multipart: true } do |f|
  = f.input :name
  = f.simple_fields_for :attachments, Attachment.new do |a|
    = a.file_field :attachment, multiple: true, name: "document[attachments_attributes][][attachment]"
  = f.submit

生成:

<input id="document_attachments_attributes_0_attachment" multiple="multiple" name="document[attachments_attributes][][attachment]" type="file">

.JS

jQuery ->
    $('#new_document').fileupload()

模型

class Document < ActiveRecord::Base
  has_many :attachments
  accepts_nested_attributes_for :attachments
end
class Attachment < ActiveRecord::Base
  belongs_to :document
  has_attached_file :attachment
end

控制器

class Member::DocumentsController < ApplicationController
  def new
    @document = Document.new
  end
  def create
    @document = Document.new params[:document]
    if @document.save
      redirect_to member_documents_path, notice: "Created"
    else
      redirect_to member_documents_path, alert: "Not created"
    end
  end
  private
  def document_params
    params.require(:document).permit(:name, attachments_attributes: [:attachment])
  end
end

我已经用两种不同的形式做了类似的事情。 基本上,您为文档创建一个表单,其中包含一个名称字段和一个用于attachment_ids的隐藏字段,然后创建一个用于附件的表单。 您可以单独上传附件(不幸的是,它们当时是孤立记录),然后使用新创建的附件记录的 ID 更新文档下的隐藏字段。

因此,基本上,从附件控制器创建一个 json 响应,包括新创建对象的 id。 然后创建一个 javascript 函数,将每个成功回调中新创建的 ID 添加到隐藏字段。

我确信有一种更简单的方法可以做到这一点,但我总是被多文件上传和嵌套属性所难倒。

编辑:所以我找到了一些旧代码并正在移植它。

class Member::AttachmentsController < Member::BaseController
  def create
    @attachment = Attachment.create!(params[:attachment])
    # TWO APPROACHES, render json view, or respond with a .js view create.js.erb
    # render json: @attachment.to_json
  end
end
class Member::DocumentsController < Member::BaseController
  def create
    @document = Document.new params[:document]
    @attachments = Attachment.find(params[:attachment_ids].split(','))
    if @document.save
      @document.attachments = @attachments
      redirect_to member_documents_path, notice: "Created"
    else
      redirect_to member_documents_path, alert: "Not created"
    end
  end
end

然后,您可以在屏幕截图中创建创建.js.erb

var screenshotContainer, 
    idContainer;
screenshotContainer = $('#attachments');
idContainer =  $('#attachment_ids_hidden_field');  
screenshotContainer.append('<%= j render @attachment %>');
idContainer.val(function(i, v) {
  var arr = v.split(', ');
  arr.push('<%= @attachment.id %>');
  return arr.join(', ');
});

例如,这可能是屏幕截图呈现调用,但在部分中根据需要显示它。

<%= image_tag(attachment.attachment, size: "100x100", data: { attachment_id:     attachment.id }) if attachment.attachment? %>

在文档窗体中创建

<input type="hidden" id="attachment_ids_hidden_field" value="" name="attachment_ids">

另一种方法是使用 json 进行响应,并在文件上传的完成回调中将新附件的 json ID 添加到隐藏字段中。

你需要解析hidden_ids的任何混乱可能比 .split(',') 更好

我没有机会太仔细地研究这个问题。

希望它有所帮助。

最新更新