我正在使用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