Rails3,S3,回形针附件,因为它自己的模式? [英] Rails3, S3, Paperclip Attachment as it's own model?

查看:142
本文介绍了Rails3,S3,回形针附件,因为它自己的模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我工作的一个应用程序,用户可以上传和管理照片与重视他们一群特定行业的元数据。

So, I'm working on an app where users can upload and manage photos with a bunch of industry specific metadata attached to them.

该照片模型的所有元数据在里面,我用回形针附加的实际图像文件到模型并存储在Amazon S3上的图像。

The Photo model has all this metadata in it, and I'm using Paperclip to attach the actual image file to the model and store the images on Amazon S3.

用户交互当前的工作原理是这样的:

The user interaction currently works like this:

  1. 在用户点击上传照片,并采取了新摄影页面,在这里,他是presented一种形式。
  2. 在表格中的第一件事情是一个文件选择。用户选择的文件。
  3. 之下这是元数据在填写用户几个不同的领域,所以用户填写这些出去。
  4. 在用户点击提交,文件上传和一个新的Photo对象被创建,将用户重定向到不同的页面。

因此​​,很明显的改进,我想要提出的是为照片实际上传在这个过程的开始,使得没有击中提交和被重定向到下一个页面之间的这样一个明显的延迟。这也将是不错的可以向用户展示他们的照片的缩略图preVIEW一旦完成上传,让他们可以看到他们投入的元数据,因为他们填写表格的照片。

So, the obvious improvement that I'd like to make is for the photo to actually upload at the beginning of this process so that there isn't such a noticeable delay between hitting submit and being redirected to the next page. It would also be nice to be able to show the user a thumbnail preview of their photo once it's done uploading, so that they can see the photo they're putting in metadata about as they fill in the form.

我想我可以做到这一点,如果我分裂图像文件到自己的模式,但我随后参照图像,像这样:

I figure I could make that happen if I split the image file into its own model, but I'd then be referring to the images like so:

@ photo.attachment.file.url ,而不是简单的 @ photo.file.url ,我用现在。我宁愿不要鸟巢它更深入比我不得不这样做。

@photo.attachment.file.url instead of the simpler @photo.file.url that I use now. I'd rather not "nest it" more deeply than I have to.

此外,分裂成两个模型提出了管理的孤儿,这是我目前还没有应对的问题。

Also, splitting it into two models raises the issue of managing orphans, which I currently don't have to deal with.

所以,我的问题是:

  1. 有没有好办法 - preferably的没有的使用Flash - 创建而不破这种异步上传行为分为两个型号,或者 -
  2. 如果我的必须的分割元数据和文件到两款车型,有没有办法让回形针治疗附件作为自己的模式,这样我可以使用< MODELNAME>< paperclip_method> 而不是< MODEL_NAME>< attachment_attribute>< paperclip_method>
  1. Is there a good way - preferably not using Flash - to create this asynchronous upload behavior without splitting into two models, OR --
  2. If I must split the metadata and the file into two models, is there a way to get Paperclip to treat an attachment as its own model so that I can access it using <modelname>.<paperclip_method> instead of <model_name>.<attachment_attribute>.<paperclip_method>?

我知道这是一个很大的问题,所以非常感谢你提前为您的帮助!

I know that's a big question, so thank you very much in advance for your help!

推荐答案

一个星期的实验后,我想我会发布什么,我终于做到了:

After a week of experimentation I just thought I'd post what I finally did:

我其实没有,因为我最后不得不与我尝试任何方法创建空记录的照片分成两种型号。我发现这是更容易,最终有两个独立的模型,因为:

I did in fact split the photo into two models, because I ended up having to create empty records with any approach I tried. I found it was easier in the end to have two seperate models because:

  1. 这使得它更容易在Rails的公约的工作(使用标准的REST动作的第二个模型来处理异步更新,而不是几个自定义操作添加到父模型)。

  1. It made it easier to work within Rails convention (use the standard REST actions for the second model to handle asynchronous updates, rather than having to add several custom actions to the parent model).

无论哪个选项我想我最后不得不孤立记录作为一种可能性。我觉得这是比较容易有一个父对象,它不会永远保存,除非有效的(照片模式在我的情况),并有附件可能是孤儿。附件从不直接调用任何地方的应用程序,所以有意外有一个空的记录中没有可能被拉升,也没有必要为了设置一个默认的范围或某事,只显示有效的照片。清理孤儿是pretty的简单,只是做 Attachment.where(:PARENT_ID =&GT;无)。并删除所有

No matter which option I tried I ended up having orphan records as a possibility. I found it was easier to have a parent object which does not ever save unless valid (the Photo model in my case), and to have attachments which may be orphans. The attachments are never called directly anywhere in the app, so there's no risk of accidentally having an empty record being pulled up, and no need to set a default scope or something in order to only show "valid" photos. Cleaning up orphans is pretty easy, just do Attachment.where( :parent_id => nil ) and delete all those.

我可以通过简单的委托附件照片保持previous code。请参阅<一href="http://stackoverflow.com/questions/4642105/what-is-a-more-ruby-like-way-of-doing-this-command/4642741#4642741">this回答的另一个问题。

I can maintain the previous code by simply delegating attachment to photo. See this answer on another question.

我希望这可以节省其他一些麻烦的道路。如果您需要Ajax功能带有附件,最好让他们自己的模式。此外,添加AJAX的文件上传到形式,请 https://github.com/formasfunction/remotipart 。这救了我的应用程序。

I hope this saves others some trouble down the road. If you need Ajax functionality with attachments it is best to make them their own model. Also, for adding ajax file upload to forms, check out https://github.com/formasfunction/remotipart. This saved my app.

这篇关于Rails3,S3,回形针附件,因为它自己的模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆