使用单个file_uploader rails 4上传多个文件

Uda*_*das 4 ruby-on-rails paperclip ruby-on-rails-4

sr_documents/form:

<%= simple_form_for @service_request,:url=>upload_document_path(@service_request.id),:remote=>true,:html => { :multipart => true } do |f| %>

<%= f.nested_fields_for :sr_documents do |q| %>

<%= q.input :file,:required => true,:hint=>"(only .pdf,.docx,.doc,.txt)", multiple: true,:name=>'file[]' %>

<%= f.button :submit ,:class=> "btn btn-go button",data: {"disable-with" => "Processing..."}%>

<%= f.add_nested_fields_link :sr_documents,"Add new file" %>
<%end%>
Run Code Online (Sandbox Code Playgroud)

我使用的宝石nested_form_fieldspaperclip我的应用程序.通过上面的代码,我可以上传多个文件.但我担心的是如何使用单个file_uploader上传多个文件.我使用的名称file[]:multiple=>true,仍然是其无法正常工作.请帮帮我.

rav*_*991 6

我也需要同样的东西,但没有任何解决方案.在我正在使用的项目中

= file_field_tag :image_files, :multiple => true, name: 'image_files[]', style: 'display:none', :id => 'fileinput'
Run Code Online (Sandbox Code Playgroud)

用户只需在选择此字段后选择多个文件.在控制器中,我能够使用params [:image_files]获取所有这些文件

然后我将图像构建为:

 # This method builds images as per image_files params
  def build_images
    (params[:image_files] || []).each do |img|
      @product.images.build(file: img)
    end
  end
Run Code Online (Sandbox Code Playgroud)

  • 我建议了一个解决方法。不声称它是正确的答案:)。 (2认同)