【问题标题】:Cloudinary URLs break with Mongoid 3?Cloudinary URL 与 Mongoid 3 中断?
【发布时间】:2012-11-06 15:25:58
【问题描述】:

我一直在使用 Cloudinary gem,到目前为止,它作为 Mongoid 中的嵌入式 Photo 模型运行良好:

class PhotoUploader < CarrierWave::Uploader::Base
  include Cloudinary::CarrierWave
  def public_id
    return model.id
  end
end

class Photo
  include Mongoid::Document
  include Mongoid::Timestamps  
  embedded_in :place#, :inverse_of => :photos, :autosave => true
  mount_uploader :image, PhotoUploader
  #...
end

但是,升级到 Mongoid 3 后,获取图像 URL 不再有效。考虑一下这个照片模型:

1.9.3p194 :019 > p
 => #<Photo _id: 507bc3c82a450b14bd00e00a, _type: nil, created_at: 2012-10-15 08:05:28 UTC, updated_at: 2012-10-15 08:05:28 UTC, image_filename: nil, caption: nil, original_url: "http://www.reactionny.com//images/assets/101755_316529.JPG", image: "v1350288333/507bc3c82a450b14bd00e00a.jpg"> 
1.9.3p194 :020 > p.image
 => /assets/fallback/default.png 
1.9.3p194 :021 > p.image_url
 => "/assets/fallback/default.png" 
1.9.3p194 :022 > p['image']
 => "v1350288333/507bc3c82a450b14bd00e00a.jpg" 

为什么它不再以http://res.cloudinary.com/XXXXXX/image/upload/v1350288842/507bc5ca2a450b14bd00e896.jpg 的形式返回完全限定的 Cloudinary URL?

【问题讨论】:

    标签: ruby-on-rails file-upload mongoid carrierwave cloudinary


    【解决方案1】:

    好像carrierwave-mongoid把保存图片的字段名从yyy改成了yyy_filename。 在您的模型检查打印中,您可以看到image_filenamenil

    与 Cloudinary GEM 和 CarrierWave 的集成似乎运行良好,但您需要迁移模型。 相关字段过去称为“图像”,现在称为“图像文件名”。您可能应该更新您的模型以使用新的字段名称并复制所有值。

    另一种可能的解决方案是使用mount_on 专门设置属性名称:

    mount_uploader :image, PhotoUploader, mount_on: :image
    

    【讨论】:

      猜你喜欢
      • 2015-10-22
      • 2014-10-22
      • 2021-02-12
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-12
      相关资源
      最近更新 更多