0
votes

I want to upload multiple images to cloudinary by association of a carrierwave active record. How can I do this in a seeds file, using an array of remote urls? Ive read countless articles how to use carrierwave/cloudinary helper tags to target an image upload within an html form, but nothing on doing this directly within code, any ideas?

1

1 Answers

0
votes

That's not so hard to do at all. So what I did:

# Gemfile
gem 'cloudinary'
gem 'carrierwave'



#config/cloudinary.yml
development:
  cloud_name: "carrierwave-example"
  api_key: "YOUR CLOUDINARY CREDENTIALS"
  api_secret: "YOUR CLOUDINARY CREDENTIALS"


# in your uploader
class ImageUploader < CarrierWave::Uploader::Base
  include Cloudinary::CarrierWave #include cloudinary lib

  # storage :file - comment this out

  # def store_dir - comment this too
  #   "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  # end
end


# You model, that uses uploader
class Picture < ActiveRecord::Base
  mount_uploader :image, ImageUploader
end

After writting this simple staff you can create Pictures, that will store images in clodinary like this:

Picture.create(image: "/path/to/image")

or if you have remote links of images, U just itterate through them

["http://image1.jpg","http://image2.jpg","http://image3.jpg"].each do |link|
  Picture.create(remote_image_url: link)
end 

Just remember to use remote_#{your_column_name}_url if you have remote link