Paperclip - 保存文件后运行方法? [英] Paperclip - Running a method after the file is saved?

查看:31
本文介绍了Paperclip - 保存文件后运行方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个需要接受文件上传的项目.文件上传后,我正在做一些处理——从文件中提取信息.我最终计划在后台工作程序中运行它,但它目前正在内联运行.

I'm working on a project that needs to accept file uploads. After the file is uploaded, I'm doing some processing - extracting information from the file. I eventually plan to run this in a background worker, but it's currently running inline.

我尝试同时使用 after_create 和 after_save 来处理文件,但我的方法似乎在 Paperclip 的 save 方法之前运行 - 所以我的测试失败并显示没有这样的文件或目录".

I've tried making use of both after_create and after_save to process the file, but it seems my method is ran before the save method from Paperclip - so my tests fail with "No such file or directory".

有没有办法提前触发保存方法,或者在文件保存到文件系统后以某种方式运行我的方法?

Is there any way to trigger the save method early, or to somehow run my method after the file has been saved to the file system?

推荐答案

您无法在回调中读取回形针文件,因为它尚未保存到文件系统(或屁股).为什么,我不太确定.

You can't read paperclip file in a callback as it's not saved to filesystem yet (or butt). Why, I'm not exactly sure.

原因是回形针通过 after_save 回调写出文件.该回调发生在 after_create

Reason is that paperclip writes out the file via after_save callback. That callback happens after after_create

但是,您可以获得用于处理的文件负载.例如:

However you can get the file payload for your processing. For example:

class Foo < ActiveRecord::Base

  has_attached_file :csv

  after_create :process_csv

  def process_csv
    CSV.parse(self.csv.queued_for_write[:original].read)
    # .. do stuff
  end

end

我必须在 2 分钟前执行此操作.希望这会有所帮助.

I had to do this 2 minutes ago. Hope this helps.

这篇关于Paperclip - 保存文件后运行方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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