Rails production - all pictures are broken after new deploy Rails production - all pictures are broken after new deploy nginx nginx

Rails production - all pictures are broken after new deploy


Ok, I found the solution. The problem appeared because I didn't change the default folder to keep images. You can find your default folder in public/uploads. That means that each cap deploy will create a new empty folder which doesn't contain your older files.

To fix this you should create another folder which doesn't live in your application. I choose the easiest way. I created symlinlk.

My steps:

1) On your server go to your app's shared folder (it generated automatically via capistrano). And then create your folder to keep new images:

$ mkdir uploads

2) Give necessary rights for a created folder:

$ sudo chmod 775 uploads

3) On your local machine in config/deploy.rb add:

task :symlink_config, roles: :app do  ...  run "ln -nfs #{shared_path}/uploads #{release_path}/public/uploads"end

4) Then push git and deploy:

$ git push$ cap deploy:symlink$ cap deploy

Now everything works fine.


Good one! I've extended your capistrano recipe.

# config/recipes/carrierwave.rbnamespace :carrierwave do   task :uploads_folder do    run "mkdir -p #{shared_path}/uploads"    run "#{sudo} chmod 775 #{shared_path}/uploads"  end  after 'deploy:setup', 'carrierwave:uploads_folder'  task :symlink do     run "ln -nfs #{shared_path}/uploads #{release_path}/public/uploads"  end  after 'deploy', 'carrierwave:symlink'end


@ExiRe & @Charlie answer works on Capistrano 2.x. In Capistrano 3.x, command run replaced with command execute.

So, I solved this with the following steps:

  1. Create a rake file in the directory lib/capistrano/tasks/carrierwave.rake with the content:
namespace :carrierwave do  task :uploads_folder do    on roles(:app) do      execute "mkdir -p #{shared_path}/uploads"      execute "#{sudo} chmod 775 #{shared_path}/uploads"    end  end    task :symlink do    on roles(:app) do      execute "ln -nfs #{shared_path}/uploads #{release_path}/public/uploads"    end  endend
  1. Add the following line at the end of your Capfile if it's doesn't have it.Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

  2. Add these tasks in config/deploy.rb with other tasks.

  • after 'deploy:publishing', 'carrierwave:uploads_folder'
  • after 'deploy:publishing', 'carrierwave:symlink'
namespace :deploy do  ..  after 'deploy:publishing', 'carrierwave:uploads_folder'  after 'deploy:publishing', 'carrierwave:symlink'  ..end
  1. Push git and deploy

Now your uploaded image will remain saved in shared/uploads folder after even new deploy.