【发布时间】:2013-10-31 06:22:06
【问题描述】:
我想通过关联载波活动记录将多张图像上传到 cloudinary。如何使用远程 url 数组在种子文件中执行此操作? 我已经阅读了无数文章如何使用carrierwave/cloudinary 辅助标签来定位html 表单中的图像上传,但没有直接在代码中执行此操作,有什么想法吗?
【问题讨论】:
我想通过关联载波活动记录将多张图像上传到 cloudinary。如何使用远程 url 数组在种子文件中执行此操作? 我已经阅读了无数文章如何使用carrierwave/cloudinary 辅助标签来定位html 表单中的图像上传,但没有直接在代码中执行此操作,有什么想法吗?
【问题讨论】:
这根本不难做到。 所以我做了什么:
# 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
【讨论】: