ruby-on-rails – 使用Carrierwave一次上传多个文件到Rails应用程序(HTML5)

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 使用Carrierwave一次上传多个文件到Rails应用程序(HTML5)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我很接近……非常接近……我能够上传单个文件……但是当我将表单的file_field类型更改为:multiple =>是的,所以我可以一次上传多个图像,我的上传文件被包裹在一个数组中……’accepts_nested_attributes_for`的’魔力’丢失了.

编辑:
经过更多的检查,我想知道我是否需要打扰accepts_nested_attributes_for?也许我应该有一个file_field,:multiple =>在我的Gallery表单中是真的(而不是嵌套表单),然后在我的创建操作中创建新的库,然后循环遍历params [:gallery] [:photos_attributes] [“0”] [:image]数组中的每个元素可以这么说,为每个元素调用@ gallery.photos.create. ?!?看起来很麻烦……但我能想到的就是这些.

希望有更多Rails经验的人可以加入……

PARAMS:

  1. {"utf8"=>"✓","authenticity_token"=>"9jXvIwcllct7UyUfo6cvhEucQf2u3SY50SuaCLtFO4c=","gallery"=>{
  2. "name"=>"First Gallery","photos_attributes"=>{"0"=>{
  3. "image"=>[
  4. #<ActionDispatch::Http::UploadedFile:0x00000104b78978
  5. @original_filename="first_test_image.jpg",@content_type="image/jpeg",@headers="Content-Disposition: form-data; name=\"gallery[photos_attributes][0][image][]\"; filename=\"first_test_image.jpg\"\r\nContent-Type: image/jpeg\r\n",@tempfile=#<File:/var/folders/bQ/bQYZC2ukFZCvbKzEDGRtJE+++TI/-Tmp-/RackMultipart20110622-4459-vz78ee>>,#<ActionDispatch::Http::UploadedFile:0x00000104b78950
  6. @original_filename="second_test_image.jpg",@headers="Content-Disposition: form-data; name=\"gallery[photos_attributes][0][image][]\"; filename=\"second_test_image.jpg\"\r\nContent-Type: image/jpeg\r\n",@tempfile=#<File:/var/folders/bQ/bQYZC2ukFZCvbKzEDGRtJE+++TI/-Tmp-/RackMultipart20110622-4459-1jzhhyg>>
  7. ]
  8. }
  9. }
  10. },"commit"=>"Save","action"=>"create","controller"=>"galleries"}
  11.  
  12.  
  13.  
  14. #app/models/gallery.rb
  15. class Gallery < ActiveRecord::Base
  16. has_many :photos,:dependent => :destroy
  17. accepts_nested_attributes_for :photos
  18. end
  19.  
  20. #app/models/photo.rb
  21. class Photo < ActiveRecord::Base
  22. belongs_to :gallery
  23. mount_uploader :photo,PhotoUploader
  24. end
  25.  
  26. #config/routes.rb
  27. resources :galleries do
  28. resources :photo,:only => [:create,:destroy]
  29. end

GalleriesController

  1. def new
  2. @gallery = Gallery.new
  3. @gallery.photos.build
  4.  
  5. respond_to do |format|
  6. format.html # new.html.erb
  7. format.json { render json: @gallery }
  8. end
  9. end
  10.  
  11. ...
  12.  
  13. def create
  14. @gallery = Gallery.new(params[:gallery])
  15.  
  16. respond_to do |format|
  17. if @gallery.save
  18. format.html { redirect_to @gallery,notice: 'Gallery was successfully created.' }
  19. format.json { render json: @gallery,status: :created,location: @gallery }
  20. else
  21. format.html { render action: "new" }
  22. format.json { render json: @gallery.errors,status: :unprocessable_entity }
  23. end
  24. end
  25. end

解决方法

你可以为params数组做一点破解,比如:
  1. aux = []
  2. params[:gallery][:photos_attributes][:image].each do |f|
  3. aux << {:image => f}
  4. end
  5. params[:post][:photos_attributes] = aux
  6.  
  7. @gallery = Gallery.new(params[:gallery])

我有类似的问题,我知道这是一个丑陋的黑客,但它对我有用.

猜你在找的Ruby相关文章