ruby 使用载波创建新JSON记录时未定义方法'map'

krcsximq  于 2022-11-04  发布在  Ruby
关注(0)|答案(2)|浏览(84)

我收到错误消息:

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
lb3vh1jj

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

@product = Product.create(
  # ActiveModel does not really care if you mix string and symbol keys
  row.slice(
    'name', 'active', 'costprice', 'category_id', 
    'price', 'provider', 'tipo', 'description'
  ).merge(
    images: ["square-1.jpg"]
  )
end
7xzttuei

7xzttuei2#

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)

      @product = Product.new(
        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: [uploader]
      )

      puts @product.images

      @product.save
      if @product.save
        puts "Saved!"
      else
        puts @product.errors.full_messages
      end
    end
  end

相关问题