关联上传与Carrierwave_Direct与对象的文件 [英] Associating a file uploaded with Carrierwave_Direct with an object

查看:111
本文介绍了关联上传与Carrierwave_Direct与对象的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我直接使用carrierwave_direct宝石上传文件到S3,一切都进展顺利。然而,当亚马逊重定向回specfie​​d在 success_action_redirect 的网址,我没有关联上传的文件与对象。

I'm uploading files directly to S3 using the carrierwave_direct gem and everything is going smoothly. However, when amazon redirects back to the URL specfied in success_action_redirect, I'm not associating the uploaded file with the object.

我的控制器看起来像

def edit
  @excel_version = ExcelVersion.find(params[:id])
  @uploader = @excel_version.excel_everest_macro
  @uploader.success_action_redirect = edit_admin_excel_version_url
end


def update
  @excel_version = ExcelVersion.find(params[:id])
  @excel_version.assign_attributes(params[:excel_version])
  @excel_version.save
end

和我有我的观点,看起来像一个隐藏的表单字段     =的form_for @excel_version办|形式|       = form.hidden_​​field:键

And I have a hidden form field in my view that looks like = form_for @excel_version do |form| = form.hidden_field :key

所以在我的更新动作我只是用 @ excel_version.assign_attributes(PARAMS [:excel_version]) 由亚马逊返回键使得它到模型就好了。然而,上传安装在@excel_version对象上没有得到URL,我不能设置一个链接,然后下载文件。

So in my update action I just use @excel_version.assign_attributes(params[:excel_version]) and the key returned by Amazon makes it into the model just fine. However, the uploader mounted on the @excel_version object doesn't get url and I can't set up a link to then download the file.

我如何可以用回我的S3参数文件与模型相关联的carrierwave通常会做的有什么建议?

Any suggestions on how I can use the S3 parameters returned to me to associate the file with the model as carrierwave would normally do?

谢谢!

P.S。在这里另一种答案建议阅读github上,我已经和已经在这方面找到无益的carrier_wave直接文档。

P.S. another answer on here suggested reading the carrier_wave direct documentation on github, which I have and have found unhelpful in this respect.

推荐答案

所以我的问题是,我有 hidden_​​field:键字段错误的形式。我错把该领域的 direct_upload_form_for @uploader 的形式,上传文件到S3上。

So my problem was that I had the hidden_field :key field on the wrong form. I mistakenly put the field on the direct_upload_form_for @uploader form that uploads the file to S3.

当然,你需要的:窗体上的关键字段,用户返回的的发布文件到S3(即在URL传递给 success_action_redirect 法)。正是在那里,S3将返回键在PARAMS,你可以将其与模型相关联。

Of course, you need the :key field on a form that the user returns to after posting the file to S3 (ie at the url passed in to the success_action_redirect method). It is there that S3 will return the key in the params and you can associate it with your model.

感谢所有的有用的建议!

Thanks for all the helpful suggestions!

这篇关于关联上传与Carrierwave_Direct与对象的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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