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