1
votes

I am building a marketplace web app, and in doing so I need multiple product images per product.

I am using Carrierwave uploader and Cloudinary to manage the images, and to the best of my knowledge I have followed the documentation correctly.

Here are some of the key code...

From the schema, creating the product table (I have tried both with and without):

  create_table "products", force: :cascade do |t|
    (...)
    t.json "photos", default: []

In the simple_form:

<%= f.file_field :photos, multiple: true %>
<%= f.input :photos_cache, as: :hidden %>

In the Product model:

class Product < ApplicationRecord
  mount_uploaders :photos, PhotoUploader

In the PhotoUploader

class PhotoUploader < CarrierWave::Uploader::Base
  include Cloudinary::CarrierWave
end

In the controller, the strong params:

  def new_product_params
    params.require(:product).permit( (...) {photos: []})

The form is posted and the new instance saved, all the files are uploaded to Cloudinary ok, but only the first image of all selected gets saved to the instance, and in a string - not the expected hash/json.

From the console - before:

 photos: []>

And after:

 photos: "image/upload/v1545381249/fdw1ydn6d1latvtlbobr.jpg">

I have seen other tutorials that have the image field as only an array and not a json, but Carrierwave documentation says to create a json field type.

1
Please show mount_uploader in your Product modal. - 孙悟空
Thanks for your interest, I have edited the question with more code examples - brian-welch
@Killuh-B you can check this thread here. I think it's a cloudinary issue. - Sovalina
@Sovalina - OUCH.... that hits me where it hurts - brian-welch
Even though the images are saved completely ok, would it still be an Cloudinary issue? - brian-welch

1 Answers

1
votes

Hope this can help you In your Product Model :

...
mount_uploader :photos, PhotoUploader
serialize :photos, JSON
...

Create another migration script :

def change
    remove_column :products, :photos
    add_column :products, :photos, :string, array: true, default: []
end