How to set CarrierWave's 'stor_dir' to a server location that will persist after re-running 'cap deploy'?

93 views Asked by At

Images I upload to my rails app with picture_uploader.rb disappear after each new 'cap deploy'.

# picture_uploader.rb

class PictureUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick
  process resize_to_limit: [500, 500]

  storage :file

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

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

Capistrano deploys my app to:

/var/www/myapp_app/

so the physical storage location resulting from the above configuration is:

/var/www/myapp_app/releases/20150608000412/uploads/micropost/production/338/example_photo.jpg

and the image path rendered in a web browser becomes:

https://example.com/uploads/micropost/production/338/example_photo.jpg

Uploaded images appear to work at first but then disappear the next time I run 'cap deploy'.

Thanks in advance for any help.

1

There are 1 answers

0
CryptoPiggy On BEST ANSWER

I figured it out. The problem was in config/deploy/production.rb. I needed to add 'public/uploads' to my linked_dirs like this:

set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system', 'public/uploads')

so that Capistrano will create the shared/public/uploads/ directory (if not present) and symlink each new deploy release to it.