【问题标题】:Check if Carrierwave image version is exist检查是否存在 Carrierwave 映像版本
【发布时间】:2015-05-02 06:58:06
【问题描述】:

使用 Carrierwave 和 Fog 将图像上传到 S3,我想检查图像版本是否存在。

我正在使用这段代码(受answer 启发)来检查它是否存在

- if post.image_url(:thumb).file.exists?
  .media-cover{:style => "background-image: url(#{post.image_url(:thumb)}"}

但是我收到了这个错误

NoMethodError at /

undefined method `file' for #<String:0x007fb7ab7af980>

我可以使用post.image_url访问默认版本

【问题讨论】:

    标签: ruby-on-rails carrierwave fog


    【解决方案1】:

    很明显,它返回的是 :thumb image_url 而不是版本本身,因此您无法获取文件。您可以检查:

    post.thumb.file.exists?
    

    thumb 文件是否存在,则使用 url helper

    【讨论】:

    • 我试过了,但我得到了undefined method thumb for,因为帖子对象没有附加thumb方法。
    • 您的 CarrierWave 设置中是否有 :thumb 条目(大小/比率等)?名称可以是您选择的任何名称;不仅仅是:拇指。
    • @MattDarby 是的,这是我为 :thumb 设置的 version :thumb do process :resize_to_fit =&gt; [312, 170] end
    猜你喜欢
    • 2014-06-28
    • 1970-01-01
    • 2017-09-06
    • 2018-11-28
    • 2012-07-07
    • 1970-01-01
    • 2020-07-08
    • 2011-03-20
    • 1970-01-01
    相关资源
    最近更新 更多