tom*_*nek 10 ruby jquery ruby-on-rails nested-attributes jquery-file-upload
我正在使用jquery-fileupload-rails来上传多个文件.
我希望能够设置文档名称并向其添加多个附件.
但是现在当我选择3个附件时,documents每个附件创建3 个附件.
我想我需要改变某种形式来添加附件.我添加了多个选项和编码名称.
我想使用这个插件,因为稍后我会想要添加拖放功能.
从
= 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
然后你可以在屏幕截图中创建一个create.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 进行响应,并在 fileupload 的完成回调中将新附件的 json ID 添加到隐藏字段。
您需要解析任何混乱的hidden_ids,这可能比仅仅解析 .split(',') 更好
我还没有机会仔细观察这个问题。
希望它有帮助。
| 归档时间: | 
 | 
| 查看次数: | 1408 次 | 
| 最近记录: |