Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在seeds文件中将Carrierwave与cloudinary一起使用_Ruby On Rails_Cloudinary - Fatal编程技术网

Ruby on rails 如何在seeds文件中将Carrierwave与cloudinary一起使用

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。如何使用远程URL数组在seeds文件中执行此操作?
我读过无数关于如何使用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