10

I am using jquery-fileupload-rails for multiple files uploading.

I want to achieve ability to set name of document and adding to it multiple attachments.

But right now when I choose 3 attachments it creates 3 documents each one with one attachment.

I guess I need to change somehow form for adding attachments. I added multiple option and harcoded name.

I want to use this plugin because later I will want to add drag-and-drop feature.

From

= 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

Generate:

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

JS

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

Models

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

Controller

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
4

1 に答える 1

3

私は2つの別々のフォームで同様のことをしました。基本的に、name フィールドと attachment_ids の非表示フィールドを含むドキュメント用のフォームを作成してから、添付ファイル用のフォームを作成します。添付ファイルを個別にアップロードし (残念ながら、その時点では孤立したレコードになります)、ドキュメントの下の非表示フィールドを、新しく作成された添付レコードの ID で更新することができます。

したがって、基本的には、新しく作成されたオブジェクトの ID を含む、添付ファイル コントローラーからの json 応答を作成します。次に、各成功コールバックから新しく作成された ID を非表示フィールドに追加する JavaScript 関数を作成します。

これを行う簡単な方法があると確信していますが、複数ファイルのアップロードとネストされた属性にいつも少し困惑しています。

編集:古いコードを見つけたので、移植しています。

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">

これを行うもう 1 つの方法は、json で応答し、fileupload の done コールバックで、新しい添付ファイルの json ID を隠しフィールドに追加することです。

おそらく単なる .split(',') よりも、hidden_​​ids の混乱を解析する必要があります。

私はこれを詳しく見る機会がありませんでした。

うまくいけば、それは役に立ちます。

于 2013-11-06T16:26:58.107 に答える