Paperclip, Rails, and Automatic AJAX Upload of an Attachment
This is a sample Rails app that has one model that has an attachment: Workout. In the Workout form, users can attach a picture. The picture is immediately uploaded to the server and a preview image shown. When the workout form is submitted, the uploaded picture is attached to the new workout record.
This is an interesting case because the attachment is uploaded before the form is submitted. What is Paperclip supposed to attach it to?
The solution in this example is to use a model designed to hold these uploaded photos: TempPhoto.
It is unavoidable that some TempPhoto records don't get cleaned up. A rake task should be created to delete all old TempPhoto records and their files. This example app isn't robust enough to include all the bells and whistles that a production-ready app will need.