2013-01-05 20 views
2

這個想法是循環控制器中的圖像數組,然後存儲它們中的每一個。請求顯示有關上傳的圖片使用carrierwave進行多次上傳而無javascript

{"utf8"=>"✓", 
"authenticity_token"=>"usbr1ma8uKWBPiVFKh8AKoGYeaZTWAlLXscMBlFYyhw=", 
"painting"=>{"name"=>"", 
"image"=>[#<ActionDispatch::Http::UploadedFile:0x007fb414becad8 @original_filename="union_creek.jpg", @content_type="image/jpeg", 
@headers="Content-Disposition: form-data; name=\"painting[image][]\"; filename=\"union_creek.jpg\"\r\nContent-Type: image/jpeg\r\n", 
@tempfile=#<File:/var/folders/80/2pcl195n7_x0l8bnrjrp4xw40000gn/T/RackMultipart20130105-7179-kr8a7e>>, 
#<ActionDispatch::Http::UploadedFile:0x007fb414beca10 @original_filename="what_lies_beneath.jpg", 
@content_type="image/jpeg", 
@headers="Content-Disposition: form-data; name=\"painting[image][]\"; filename=\"what_lies_beneath.jpg\"\r\nContent-Type: image/jpeg\r\n", 
@tempfile=#<File:/var/folders/80/2pcl195n7_x0l8bnrjrp4xw40000gn/T/RackMultipart20130105-7179-iclmmw>>]}, 
"commit"=>"Create Painting", 
"gallery_id"=>"13"} 

正確的信息但是我有一個誤導性的錯誤,當carrierwave嘗試存儲這些圖像

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id 

視圖

= form_for [@gallery, @gallery.paintings.new] do |f| 
    %p= f.text_field :name, placeholder: "Enter painting name" 
    %p= f.file_field :image, multiple: true 
    %p= f.submit %> 

控制器

uploader = ImageUploader.new 
params[:painting][:image].each do |img| 
    uploader.store!(img) # crash here 
end 

我看到控制器出了問題,但我無法想象任何更好的方式來存儲與載體的多個文件。

任何人都可以指出我嗎?由於

+0

你解決這個問題? – ExiRe

回答

1

這是任何解決方案有同樣的問題

params[:painting][:image].map { |img| gallery.paintings.create(name: img.original_filename , image: img) 
+1

問題解決了嗎?如果是,請勾選答案!另外,如果你有一個倉庫,我想看看。 =) – Apollo