0

我想通过载波活动记录的关联将多个图像上传到云端。如何使用远程 url 数组在种子文件中执行此操作?我已经阅读了无数文章如何使用carrierwave/cloudinary 辅助标签来定位html 表单中的图像上传,但没有直接在代码中执行此操作,有什么想法吗?

4

1 回答 1

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如果您有远程链接,请记住使用

于 2013-10-22T12:52:51.070 回答