载波和 mongoid 的回调问题
我在 Rails 3 应用程序上使用 Carrierwave 和 mongoid,并且遇到了 after_save 回调的问题。考虑以下问题
class Video
include Mongoid::Document
field :name
mount_uploader :file, VideoUploader
after_create :enqueue_for_encoding
protected
def enqueue_for_encoding
// point your encoding service to where it expects the permanent file to reside
// in my case on s3
end
end
我的问题是,在我的 enqueue_for_encoding
方法中,file.url 指向本地 tmp 目录而不是 s3 目录。
当 file.url 指向 s3 时,如何调用 enqueue_for_encoding
方法?
谢谢!
乔纳森
I am using carrierwave and mongoid on a rails 3 application and am having an issue with an after_save callback. Consider the following
class Video
include Mongoid::Document
field :name
mount_uploader :file, VideoUploader
after_create :enqueue_for_encoding
protected
def enqueue_for_encoding
// point your encoding service to where it expects the permanent file to reside
// in my case on s3
end
end
My issue is that in my enqueue_for_encoding
method, file.url points to the local tmp directory not the s3 directory.
How do I get my enqueue_for_encoding
method to be called when file.url points to s3?
Thanks!
Jonathan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看有关回调的 Carrierwave 操作指南页面
https://github.com/ jnicklas/rierwave/wiki/How-to%3A-use-callbacks
它对我有用
Check out carrierwave's howto page on Callbacks
https://github.com/jnicklas/carrierwave/wiki/How-to%3A-use-callbacks
It worked for me
好吧,我明白了。需要进行一些黑客攻击。因此,目前 Carrierwave 不公开 after_create 钩子,所有的持久化和处理都发生在 after_save 回调中。这是我用来解决这个问题的代码:
更新
哎呀——这不起作用。差一点就成功了——网址是正确的,但它被永久解雇了。意味着文件仍在加载过程中,并且在调用 enqueue_for_encoding 时尚未完全存储
Okay, I figured it out. To took a bit of hacking. So currently carrierwave does not expose an after_create hook, all of it persisting and processing happens in the after_save callback. Here is the code I used to work around it:
UPDATE
Woops -- that didn't work. It almost did -- the url is correct, but it is being fired permanently. Meaning the file is still in process of being loaded, and is not fully stored when enqueue_for_encoding is called
可以在上传器本身上设置
enqueue_for_encoding
回调。但我更喜欢这样做:It is possible to set your
enqueue_for_encoding
callback on the uploader itself. But I prefer to do it this way:您可以尝试删除模型中的
after_create
回调,并将以下内容添加到您的上传器:process
回调在文件保存后调用(我认为)这应该允许您在文件上传到 S3 后挂载。You could try removing your
after_create
callback in the model and add the following to your uploader:The
process
callbacks are called after the file is saved (I think) which should allow you to hook in once your file is up on S3.