Search code examples
ruby-on-railsrubyimagecapistranorubber

Carrierwave uploaded Images aren't persistent after Capistrano deploy


I've just noticed that after I redeploy my rails app to production with cap deploy:migrations any image that I've uploaded via my admin forms (such as creating a testimonial with an avatar image) that the image links are now broken. The images appear fine as long as I don't redeploy any code, which is not desired since I push code changes quite frequently. I assume this is related to the way capistrano creates the file structure in 'releases' for each deployment but I'm not sure how to go about fixing this issue.

I'm also not tracking public/uploads with git since I don't want the fake content I use on localhost to appear in production.

So, before my latest code push, I had all the images there since I just added them. Now, after the push there are no images:

broken image links

Here are the files that I believe are relevant (let me know if there is one that you need to see beyond these):

avatar_uploader.rb:

class AvatarUploader < CarrierWave::Uploader::Base
  include CarrierWave::RMagick
  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  process :resize_to_fit => [200, 200]

  def extension_white_list
    %w(jpg jpeg gif png)
  end
end

Solution

  • By default, Capistrano links public/system directory. So to persist your images, just change

    def store_dir
      "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    end
    

    to

    def store_dir
      "system/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    end