【问题标题】:Rails + Mongoid + Carrierwave + CloudinaryRails + Mongoid + Carrierwave + Cloudinary
【发布时间】:2015-10-22 08:06:28
【问题描述】:

我正在从 Uploadcare 迁移到 Cloudinary。

但是当我尝试更新文件字段时,该字段设置为“old

p = Photo _id: 556d225e69702d45b0000000, created_at: 2015-06-02 03:26:22 UTC, updated_at: 2015-06-02 03:26:22 UTC, file: nil
p.file = p.id
p.save

返回

Photo _id: 556d225e69702d45b0000000, created_at: 2015-06-02 03:26:22 UTC, updated_at: 2015-07-30 19:41:12 UTC, file: "_old_"


p.file = <PhotoUploader:0x007fe96fc86800 @model=#<Photo _id: 556d225e69702d45b0000000, created_at: 2015-06-02 03:26:22 UTC, updated_at: 2015-07-30 19:41:12 UTC, file: "_old_">, @mounted_as=:file, @stored_public_id=nil, @stored_version=nil, @file=nil, @original_filename=nil, @public_id=nil, @storage=#<Cloudinary::CarrierWave::Storage:0x007fe9701f8f18 @uploader=#<PhotoUploader:0x007fe96fc86800 ...>>>

有什么建议吗?

【问题讨论】:

    标签: ruby-on-rails carrierwave cloudinary uploadcare


    【解决方案1】:

    如果你保存模型时没有给上传者字段赋值,然后再赋值,CarrierWave 会临时将set the field_old_ 强制Mongoid 存储字段。一旦模型被持久化,就会分配正确的值:

    001 > p = Photo.new title: 'Out with the old!'
     => #<Photo _id: 55c76e895d8b01bef0000000, title: "Out with the old!", image: nil, clean: nil>
    002 > p.save
     => true
    003 > p
     => #<Photo _id: 55c76e895d8b01bef0000000, title: "Out with the old!", image: nil, clean: nil>
    004 > p.image = File.open( 'giraffe-small.jpg')
     => #<File:giraffe-small.jpg>
    005 > p
     => #<Photo _id: 55c76e895d8b01bef0000000, title: "Out with the old!", image: "_old_", clean: nil>
    006 > p.save
     => true
    007 > p
     => #<Photo _id: 55c76e895d8b01bef0000000, title: "Out with the old!", image: "image/upload/v1439133413/dyirrctc3ls3ficre7hj.jpg", clean: nil>
    

    请注意,Cloudinary 上传器接受图像标识符或文件对象(见下文)。

    型号

    class Photo
      include Mongoid::Document
      field :title, type: String
      mount_uploader :image, ImageUploader
    end
    

    上传者

    class ImageUploader < CarrierWave::Uploader::Base
      include Cloudinary::CarrierWave
    end
    

    存储对已上传到您的 Cloudinary 帐户的图像的引用

    如果您的图片网址是http://res.cloudinary.com/tocker/image/upload/v1435050504/sample.jpg*

    p = Photo.new
    p.title = "Existing image"
    signature = Cloudinary::Utils.api_sign_request({:public_id=>"sample", :version=>"1435050504"}, Cloudinary.config.api_secret)
    p.image = "image/upload/v1435050504/sample#{signature}"
    p.save
    

    *图片地址在你的Cloudinary media library.

    上传本地图片文件并存储参考

    p = Photo.new
    p.title = "Uploaded local image"
    p.image = File.open( path_to_local_image_file)
    p.save
    

    【讨论】:

      【解决方案2】:

      我发现了问题!

      Carrierwave 不允许你更改上传栏如果之前没有上传图片。

      所以...您必须先上传示例图片

      p = Photo.find(:id)
      p.remote_file_url = "http:sample.com/image.png"
      p.save
      

      然后...将您的 photo_uploader.rb 更改为 Cloudinary 选项

      include Cloudinary::CarrierWave
      

      最后……

      p.file = 'cloudinary public id'
      p.save
      

      还有 GOTCHA!

      【讨论】:

        猜你喜欢
        • 2018-06-09
        • 2016-12-22
        • 2013-06-17
        • 2018-06-23
        • 1970-01-01
        • 1970-01-01
        • 2018-05-19
        • 2012-09-15
        • 1970-01-01
        相关资源
        最近更新 更多