1

Using Carrierwave and Fog to upload images to to S3, I want to check if image version is exist.

I'm using this code (inspired by this answer) to check if it's exist or not

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

But I'm getting this error

NoMethodError at /

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

I can access the default version with post.image_url

Community
  • 1
  • 1
Ahmad Ajmi
  • 7,007
  • 3
  • 32
  • 52

1 Answers1

2

Well obviously it is returning the :thumb image_url and not the version itself, thus you cant get a file. You can just check with:

post.thumb.file.exists?

whether the thumb file exists, then use the url helper

Figedi
  • 383
  • 1
  • 2
  • 11
  • I tried this but I get `undefined method thumb for` as there is not `thumb` method attached to the post object. – Ahmad Ajmi May 02 '15 at 08:58
  • Do you have an entry for :thumb (size/ratio, etc) in your CarrierWave setup? The name could be anything you chose; not just :thumb. – Matt Darby May 04 '15 at 11:59
  • @MattDarby Yes this is my setup for `:thumb` as `version :thumb do process :resize_to_fit => [312, 170] end` – Ahmad Ajmi May 08 '15 at 08:24