2

这个想法是在控制器中循环图像数组,然后存储它们中的每一个。该请求显示有关上传图像的正确信息

{"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

我发现控制器有问题,但我想不出更好的方法来使用运营商存储多个文件。

谁能指出我?谢谢

4

1 回答 1

1

这是任何有同样问题的人的解决方案

params[:painting][:image].map { |img| gallery.paintings.create(name: img.original_filename , image: img)
于 2013-01-19T16:23:33.453 回答