简体   繁体   中英

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:

损坏的图片链接

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

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

Seems like you don't configure linked_dirs variable in your deploy.rb (in case of Capistrano 3 ) or not specify sym-link to your public/uploads from shared/public/uploads (in case of Capistrano 2).

Without it all deploy will "override" public/uploads directory.

Here is more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM