Prabhakaran
Prabhakaran

Reputation: 3999

create symlink to upload images using paperclip and rails

I am using paperclip in my rails application to upload images. As all know by default paperclip creating public directory in rails path.

But I face a trouble while deploying my application with capistrano. Whenever I deploy my code using capistrano it is replacing my uploads directory. So I am trying to implement symlink to create a short since I am not expert in linux I am unable to continue with it can anyone help me how to solve it

has_attached_file :upload,styles: 
{ thumb: ["150x100#",:jpg], small: ["75x75#",:png]},
  default_url: '/assets/avatar.jpg', 
  url: "/post_images/post_:post_id/:style/:filename"

Here is my url how can i create a symlink for this directory /var/uploads/post_:post_id...

Upvotes: 1

Views: 1645

Answers (2)

muhammadn
muhammadn

Reputation: 330

Using Capistrano 3.

  desc 'Persist Paperclip uploads'
  task :create_symlinks do
   on roles(:app) do
    execute "ln -nfs #{shared_path}/system #{release_path}/system" #Create symlink for private files
   end
  end

  after :published, "deploy:create_symlinks"

Upvotes: 0

Zh Kostev
Zh Kostev

Reputation: 588

There "right" way to do this is to save files in public/upload/.. directory and ignore upload folder from git. Then create symlink from release current directory to shared directory(MOVE uploads folder first to shared). Add this to deploy.rb:

namespace :deploy do
  task :create_symlinks, :role => :app do
    run "ln -nfs #{shared_path}/uploads #{release_path}/public/uploads" #Create symlink for public files
    run "ln -nfs #{shared_path}/system #{release_path}/system" #Create symlink for private files
    run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml" #Create symlink for database
    run "ln -nfs #{shared_path}/.rvmrc #{release_path}/.rvmrc"  #Create symlink for rvm
  end
end

before "deploy:finalize_update", "deploy:create_symlinks"

Upvotes: 2

Related Questions