Search code examples
ruby-on-railscarrierwaveactioncontroller

In Rails 5.2, using Carrierwave gem how can I use the default gem when the file no longer exists on disk?


I'm using Carrierwave gem on Rails 5.2.2.1. I have it setup with a default url to an image when one doesn't exist in the database for that record. But how can I have it also show up if I have an image in the database for the record, but that file doesn't exist on the disk?

Currently, I get the following error:

ActionController::RoutingError (No route matches [GET] "/uploads/player/314c38a9-6567-4faa-bfda-9e7a75cbaed4.jpeg")

Is there a way to rescue and present the default url I've specified in the Uploader file?


Solution

  • if you have a default image, you can set method in your model like this

    def image
      File.file?(file.path) ? file.url : default_image_url #and you need to define this default url
    end
    

    Or you can handle 404 on an image as @mroach mentioned in your routes