502502
502502

Reputation: 437

Rails 4: upload attachments to Amazon S3 before saving model

I have a feature where users can add Notes, and every note can have multiple file Attachments.

When a user clicks 'upload attachment', the file begins uploading to Amazon S3 instantly (before users actually save the note). This is all done with AJAX.

What is the best way (flow) to make sure these AmazonS3 files are eventually linked to the Note (when the note is submitted) or discarded if the note isn't saved?

Upvotes: 1

Views: 150

Answers (1)

Tyler
Tyler

Reputation: 11509

I can think of 2 approaches:

  1. Go ahead and save the note, but have a column on the model that tells you whether or not it's temporary. If the user clicks save, then you change that column from temporary to permanent. You could run a rake task in the background to clear out temporary notes that are more than a day old, etc., if you wanted to do that.

  2. Put some sort of reference in the image's file name. You can't use an id, since that won't exist yet, but you may have other unique identifiers such as a combination of username and post title.

If you can do the 1st option, I'd say it is much preferable. It gives you a unique ID for the note, which has the express purpose of making it possible to reference the note externally.

Upvotes: 1

Related Questions