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.
mount_uploaderin your Product modal. - 孙悟空