【问题标题】:How can I use Carrierwave with cloudinary in seeds file如何在种子文件中使用带有 cloudinary 的 Carrierwave
【发布时间】:2013-10-31 06:22:06
【问题描述】:

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

【问题讨论】:

    标签: ruby-on-rails cloudinary


    【解决方案1】:

    这根本不难做到。 所以我做了什么:

    # 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

    【讨论】:

    • remote_#{your_column_name}_url 是我所缺少的,谢谢! :)
    猜你喜欢
    • 2019-01-24
    • 2015-12-05
    • 2013-06-17
    • 2018-06-23
    • 1970-01-01
    • 2019-05-15
    • 2015-10-22
    • 2018-05-19
    • 2013-08-04
    相关资源
    最近更新 更多