0

我想在我的 Rails 应用程序中使用出色的 jQuery File Upload 上传多张图片。

所以我关注了Railscasts

  • 添加jquery-fileupload-rails gem
  • 在我的 application.js 中添加一个要求
  • 为我的图片创建表单
  • 在名为“housings.coffee”的文件中初始化 FileUpload

但是我不明白为什么当我尝试上传多张图片时没有发生任何事情,无论是使用拖放还是单击“添加文件...”。

当我按下“提交”按钮时,每张图片都会正常保存。

拜托,你能帮帮我吗?我真的被困住了。

非常感谢!

应用程序.js ```

//= require jquery
//= require jquery_ujs
//= require jquery-fileupload/basic
//= require housings

住房.咖啡

$(document).ready( ->
  jQuery ->
    file = $('fileupload').fileupload
    console.log(file)
)

外壳/_form.html.erb

<%= form_for [@user, @picture], html: { multipart: true, id: 'fileupload'} do |f| %>
      <!-- The fileupload-buttonbar contains buttons to add/delete files and start/cancel the upload -->
      <div class="row fileupload-buttonbar">
        <div class="span7">
          <!-- The fileinput-button span is used to style the file input field as button -->
          <span class="btn btn-success fileinput-button">
            <i class="icon-plus icon-white"></i>
            <span>Add files...</span>
            <%= f.file_field :image, multiple: true, name: 'picture[image]' %>
          </span>
            <%= f.hidden_field :housing_id, value: @housing.id %>
          <button type="submit" class="btn btn-primary start">
            <i class="icon-upload icon-white"></i>
            <span>Start upload</span>
          </button>
          <button type="reset" class="btn btn-warning cancel">
            <i class="icon-ban-circle icon-white"></i>
            <span>Cancel upload</span>
          </button>
          <!--<button type="button" class="btn btn-danger delete">-->
            <!--<i class="icon-trash icon-white"></i>-->
            <!--<span>Delete</span>-->
          <!--</button>-->
          <input type="checkbox" class="toggle">
        </div>
        <div class="span5">
          <!-- The global progress bar -->
          <div class="progress progress-success progress-striped active fade">
            <div class="bar" style="width:0%;"></div>
          </div>
        </div>
      </div>
      <!-- The loading indicator is shown during image processing -->
      <div class="fileupload-loading"></div>
      <br>
      <!-- The table listing the files available for upload/download -->
      <table class="table table-striped"><tbody class="files" data-toggle="modal-gallery" data-target="#modal-gallery"></tbody>
      </table>
  <% end %>

Housings_controller.rb > 索引

@picture = Picture.new(housing_id: @user.housings.first.id)
4

1 回答 1

0

不知道这是否可以帮助任何人,但我最终实现了另一个 javascript 工具Dropzone

我认为这会更好,因为 FileUpload 现在看起来很旧,而且我很难让它工作。

Dropzone 与 Rails 配合得非常好,看起来很漂亮。作为参考,我遵循了本教程

于 2015-12-07T15:35:11.937 回答