Dealing with user uploaded files between deployments
Monday, March 24th, 2008If you are writing an application that allows your users to upload files, you have to work around the fact that their files will be deleted every time you deploy your application.
Usually if you want your file to available they will be uploaded to the public folder. The solution is to store those files in the “shared” directory.
Here is one example on how to set that up for a Rails app.
Create a directory called “uploads” in your.
When using attachment_fu or similar, use a :prefix=>”/public/uploads/#{tablename}
Add the following tasks to your deploy file
desc "link upload directory"
task :symlink_uploads, :roles => :app do
run "ln -nfs /var/www/apps//containers/rails//shared/system/uploads /public/uploads"
end
end
desc "create upload directory"task :create_uploads_directory, :roles => :app do
run "mkdir #{deploy_to}/shared/system/media"
end
end
Hook them into your deployment
after "deploy","symlink_uploads"
after "deploy:setup","create_uploads_directory"
Any uploads will now be stored outside the versioned directories.