【发布时间】:2016-03-08 18:41:13
【问题描述】:
我需要在上传附件时访问 url,因此我必须定义 after_flush_writes 方法,就像这里建议的 https://github.com/thoughtbot/paperclip/issues/816
我怎样才能做到这一点?
到目前为止,我已经这样做了,但它不起作用:
class ExcelFile < ActiveRecord::Base
belongs_to :insertion
has_attached_file :excel
validates_attachment_content_type :excel, content_type: [ "application/vnd.ms-excel",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"]
def after_flush_writes
super
byebug
file = Roo::Excelx.new(self.excel.url(:original, timestamp: false))
input_from_generals self.id, file
input_from_financials self.id, file
end
我尝试使用after_commit、after_create 回调来访问self.excel.url,但它不起作用。
【问题讨论】:
-
after_flush_writes必须在Paperclip::Attachment class的上下文中定义,否则不会被调用。但是,我的测试表明,简单的after_create或after_save回调可以正常工作 - 您应该能够在这些回调中使用url。 -
谢谢! @BoraMa 我无法使用 after_create 或 after_save 执行此操作,我认为这是因为回形针通过 after_save 回调写出文件。
-
我的问题与stackoverflow.com/questions/19960471/…类似,但我不知道他们是如何解决问题的。
-
您链接的问题不同 - OP 需要对 物理文件 执行某些操作,因此他需要在回调时将其刷新到磁盘。您只需要在我的测试中甚至在将文件物理保存到磁盘之前就可以正常工作的 URL。你的情况到底是什么问题?
-
不,我还需要物理文件。 URL 已正确生成,我能够正确生成,但我收到一条错误消息“找不到文件”,因为我需要物理文件来读取一些数据。
标签: ruby-on-rails ruby ruby-on-rails-4 paperclip