Home > database >  Undefined method `map' when creating new JSON record when using Carrierwave
Undefined method `map' when creating new JSON record when using Carrierwave

Time:10-04

I'm getting the error:

undefined method `map' for #<String:0x000000011fe89ef8> Did you mean? tap

When trying to create a new record.

Example URL from my CSV

//cdn.shopify.com/s/files/1/0613/5349/2701/products/18_720x.jpg?v=1648711965

My schema:

t.json "images"

Importer view

  <div >
    <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>

Product.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

CodePudding user response:

When you have multiple uploads Carrierwave expects an array. This is determined by the name you pass to mount_uploaders:

Make sure that you mount the uploader with write (mount_uploaders) with s not (mount_uploader) in order to avoid errors when uploading multiple files

Make sure your file input fields are set up as multiple file fields. For example in Rails you'll want to do something like this:
<%= form.file_field :avatars, multiple: true %>

Also, make sure your upload controller permits the multiple file upload attribute, pointing to an empty array in a hash. For example:
params.require(:user).permit(:email, :first_name, :last_name, {avatars: []})

See: 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

CodePudding user response:

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
  • Related