Ruby on rails 如何在seeds文件中将Carrierwave与cloudinary一起使用
我想通过carrierwave活动记录的关联将多个图像上载到cloudinary。如何使用远程URL数组在seeds文件中执行此操作?Ruby on rails 如何在seeds文件中将Carrierwave与cloudinary一起使用,ruby-on-rails,cloudinary,Ruby On Rails,Cloudinary,我想通过carrierwave活动记录的关联将多个图像上载到cloudinary。如何使用远程URL数组在seeds文件中执行此操作? 我读过无数关于如何使用carrierwave/cloudinary helper标记在html表单中以图像上载为目标的文章,但没有任何关于直接在代码中执行此操作的文章,有什么想法吗?这一点也不难做到。 所以我所做的是: # Gemfile gem 'cloudinary' gem 'carrierwave' #config/cloudinary.yml d
我读过无数关于如何使用carrierwave/cloudinary helper标记在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
或者,如果你有图像的远程链接,你只需通过它们进行记录
["http://image1.jpg","http://image2.jpg","http://image3.jpg"].each do |link|
Picture.create(remote_image_url: link)
end
只要记住使用remote#{your_column_name}{url
如果您有远程链接remote#{your_column_name}{url是我缺少的,谢谢!)
["http://image1.jpg","http://image2.jpg","http://image3.jpg"].each do |link|
Picture.create(remote_image_url: link)
end