我想通过关联carrierwave活动记录将多个图像上传到cloudinary。如何使用远程网址数组在种子文件中执行此操作? 我读了无数文章如何使用carrierwave / cloudinary helper标签来定位html表单中的图像上传,但没有任何关于在代码中直接执行此操作的任何想法?
答案 0 :(得分:0)
这根本不是那么难。 所以我做了什么:
# 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
在写完这些简单的工作人员之后,您可以创建图片,这将以clodinary存储图像:
Picture.create(image: "/path/to/image")
或者如果您有图像的远程链接,则只需通过它们进行迭代
["http://image1.jpg","http://image2.jpg","http://image3.jpg"].each do |link|
Picture.create(remote_image_url: link)
end
如果您有远程链接
,请记住使用remote_#{your_column_name}_url