0

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

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
6
  • how are you attaching images to products? Are you using Rails ActiveStorage? Commented Oct 2, 2022 at 23:29
  • I'm using Carrierwave! Commented Oct 2, 2022 at 23:45
  • 1
    I'm inferring that you're uploading multiple images per product, so images should be an array, @product = Product.new(.... , images: ["square-1.jpg"], ...) Commented Oct 3, 2022 at 0:06
  • can you please add console params to questions? Commented Oct 3, 2022 at 7:54
  • @LesNightingill still getting the error.... I updated the code above Commented Oct 3, 2022 at 18:16

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks max, I'm still getting the error, I've updated my code so you can see full action
0
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.