简体   繁体   English

如何同时上传多张照片? Ruby on Rails

[英]How upload multiple photos at the same time?? Ruby on Rails

i have a problem with upload photos. 我上传照片有问题。

In muy view, i take some photo as below: 在muy视图中,我拍了一些照片,如下所示:

<%= f.label :images_urls, 'image' %> <br>
    <%= f.file_field :images_urls, {:multiple => true} %> <br>

But I don't know how save them in controller to my model "Furniture". 但我不知道如何将它们保存在控制器中的模型“家具”中。 This is my furniture, but i don't work: 这是我的家具,但是我不工作:

 def create
    image_io = params[:furniture][:images_urls]
    File.open(Rails.root.join('public','furnitures', image_io.original_filename), 'wb') do |file|
      file.write(image_io.read)
    end

    @furniture = Furniture.create(params[:furniture])

    redirect_to admins_path
  end

Have you any suggestion?? 你有什么建议吗?

Thank you very much 非常感谢你

I would suggest using a gem such as Carrierwave or Paperclip . 我建议使用诸如CarrierwavePaperclip之类的宝石。 In your view, to allow multiple file upload, you may need to implement asynchronous file upload. 在您看来,要允许多个文件上传,您可能需要实现异步文件上传。 A very handy way to do this that uses jQuery is a plugin called jQuery-file-upload (which can handle multiple files). 使用jQuery的一种非常方便的方法是使用一个名为jQuery-file-upload的插件(可以处理多个文件)。

Ryan Bates has an excellent video on Railscasts jQuery File Upload on how to do this. Ryan Bates在Railscasts jQuery File Upload上有一个很棒的视频,介绍了如何执行此操作。 There's also a gem that prepackages this jQuery plugin for you, called jquery-fileupload-rails . 还有一个为您预打包此jQuery插件的gem,称为jquery-fileupload-rails

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM