简体   繁体   English

通过载波上传多文件

[英]Multi file upload with carrierwave

I recently watched Ryan`s episode 381 about Jquery file upload and now I wonder how to add support of nested forms. 我最近看了Ryan的第381集有关Jquery文件上传的信息,现在我想知道如何添加对嵌套表单的支持。 Say I have product and products has many images. 说我有产品,产品有很多图像。 What should I do to add this without polymorphic association? 我应该怎么做才能添加没有多态关联的内容?

It isn't clear exactly what you're asking, or what polymorphic associations have to do with this, but I think I may have had the same problem (though I used plupload instead) 目前尚不清楚您在问什么,或与此相关的多态关联,但是我想我可能也遇到了同样的问题(尽管我改用了plupload)

My solution was to generate a short random string (I used SecureRandom.hex(6) , but I'm sure there are other ways to do it) and include it in my product form view. 我的解决方案是生成一个短随机字符串(我使用SecureRandom.hex(6) ,但是我敢肯定还有其他方法可以做到)并将其包含在我的产品表单视图中。 I included it as a hidden field in the form and also as a javascript variable. 我将其作为表单中的隐藏字段以及javascript变量包含在内。

<%= javascript_tag do %>
  window.tmp_id = "<%= tmp_id %>"
<% end %>

In my plupload config I added this variable to the POST request that uploads the image. 在我的plupload配置中,我将此变量添加到了上传图片的POST请求中。 For jQuery file upload I think you'd want something like this: 对于jQuery文件上传,我想您需要这样的东西:

$('#fileupload').fileupload({
  url: '/path/to/upload/handler',
  formData: { tmp_id: tmp_id }
});

So in the controller that handles the file uploads, I just take the file and stick it in a temporary directory named using the tmp_id passed in via params. 因此,在处理文件上传的控制器中,我只是将文件粘贴到使用通过params传递的tmp_id命名的临时目录中。

When the controller receives the form submission it uses the model to check for the existence of the temp directory and processes the images found inside there, creating new records for each one that are tied to the newly created Product. 当控制器收到表单提交时,它将使用模型检查temp目录是否存在,并处理在其中找到的图像,从而为每个与新创建的产品相关的记录创建新记录。

May not be exactly what you're looking for but thought it might give you some ideas. 可能不是您要找的东西,但认为它可能会给您一些想法。

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

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