我收到错误消息:
undefined method `map' for #<String:0x000000011fe89ef8> Did you mean? tap
尝试创建新记录时。
CSV中的示例URL
//cdn.shopify.com/s/files/1/0613/5349/2701/products/18_720x.jpg?v=1648711965
我的架构:
t.json "images"
导入程序视图
<div class="importer">
<h2>Importar productos a catálogo</h2>
<%= form_tag importador_path, multipart: true do %>
<%= file_field_tag :images, multiple: true %>
<%= submit_tag "Importar", class: "btn btn-primary" %>
<% end %>
<%= @importerrors %>
</div>
产品.rb
def self.my_import(file)
CSV.foreach(file.path, headers: true) do |row|
images = "https://" + row['images']
uploader = ImagesUploader.new
uploader.download! images
output = uploader.store!(images)
finalimage = uploader.url[0]
@product = Product.create(
name: row['name'],
active: row['active'],
costprice: row['costprice'],
category_id: row['category_id'],
price: row['price'],
provider: row['provider'],
tipo: row['tipo'],
description: row['description'],
images: finalimage
)
puts @product.images
@product.save
if @product.save
puts "Saved!"
else
puts @product.errors.full_messages
end
end
end
2条答案
按热度按时间lb3vh1jj1#
当您有多个上传时,Carrierwave需要一个数组。这由您传递给
mount_uploaders
的名称决定:请确保使用write(mount_uploaders)而不是(mount_uploader)装载上载程序,以避免在上载多个文件时出错
确保你的文件输入字段被设置为多个文件字段。例如在Rails中,你会想做类似这样的事情:
<%= form.file_field :avatars, multiple: true %>
个此外,确保上传控制器允许多文件上传属性,指向散列中的空数组。例如:
params.require(:user).permit(:email, :first_name, :last_name, {avatars: []})
请访问:https://github.com/carrierwaveuploader/carrierwave#multiple-file-uploads
7xzttuei2#