1

i have a problem with upload photos.

In muy view, i take some photo as below:

<%= 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

4

1 回答 1

0

我建议使用诸如CarrierwavePaperclip之类的宝石之类的宝石。在您看来,要允许多个文件上传,您可能需要实现异步文件上传。使用 jQuery 的一个非常方便的方法是一个名为 jQuery-file-upload 的插件(它可以处理多个文件)。

Ryan Bates 有一个关于 Railscasts jQuery File Upload的精彩视频,介绍了如何做到这一点。还有一个 gem 可以为你预打包这个 jQuery 插件,叫做jquery-fileupload-rails

于 2013-06-15T21:59:44.740 回答