简体   繁体   English

Rails 4:在保存模型之前将附件上传到Amazon S3

[英]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). 当用户单击“上传附件”时,文件会立即立即上传到Amazon S3(在用户实际保存便笺之前)。 This is all done with AJAX. 这一切都是用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? 确保这些AmazonS3文件最终链接到便笺 (提交便笺时)或如果不保存便笺将其丢弃的最佳方法(流程)是什么?

I can think of 2 approaches: 我可以想到2种方法:

  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. 如果您想这样做,可以在后台运行rake任务以清除超过一天的临时笔记等。

  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. 您无法使用ID,因为该ID尚不存在,但您可能还有其他唯一标识符,例如用户名和帖子标题的组合。

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. 它为您提供了注释的唯一ID,其明确的目的是使从外部引用注释成为可能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM