在Rails 3中上传多个文件 [英] Uploading multiple files in rails 3

查看:181
本文介绍了在Rails 3中上传多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在上载多个文件,但是我没有在表单和控制器之间正确传递参数-不太确定我缺少什么.

I'm working on uploading multiple files but I'm not passing the params correctly between the form and the controller - not quite sure what I'm missing.

表格非常简单:

<%= form_tag({:controller => :admins, :action => :upload_json}, {:multipart => true, :method => :post, :html => { :class => "form-horizontal"}}) do %>

  <fieldset>
    <legend>Data</legend>
    <ol class="field-list">
      <li>
        <label for="data-file">JSON File</label>
        <%= file_field_tag 'jsonfileupload', multiple: true %>
      </li>
    </ol>


    <div class="form-actions">
      <%= submit_tag "Upload" %>
    </div>

  </fieldset>

<% end %>

在控制器中,我只是这样做以查看传递的内容:

In the controller, I'm just doing this to see what's passed:

@output = params[:jsonfileupload]

在视图中,我只是执行debug来查看返回的内容,它仅提取任何一组选定文件中的最后一个文件.

In the view, I just do a debug to see what's returned, and it only pulls the last file of any set of selected files.

#<ActionDispatch::Http::UploadedFile:0x007f95d0c21010 @original_filename="4987.json", @content_type="application/json", @headers="Content-Disposition: form-data; name=\"jsonfileupload\"; filename=\"4987.json\"\r\nContent-Type: application/json\r\n", @tempfile=#<File:/var/folders/0p/6lq88m950mgftng1qm1w63_8000194/T/RackMultipart20121114-389-t9l7vs>>

不确定要返回所有选定的文件我缺少什么.感谢您的帮助!

Not sure what I'm missing to get it to return all the files selected. Thanks for the help!

推荐答案

我能够通过将file_field_tag调整为:<%= file_field_tag 'jsonfileupload[]', multiple: true %>

I was able to solve this by adjusting the file_field_tag to: <%= file_field_tag 'jsonfileupload[]', multiple: true %>

这篇关于在Rails 3中上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆