9

该指南说我可以将附件保存到光盘上以在其上运行如下进程:

message.video.open do |file|
  system '/path/to/virus/scanner', file.path
  # ...
end

我的模型有一个附件定义为:

has_one_attached :zip

然后在我定义的模型中:

def process_zip      
  zip.open do |file|
    # process the zip file
  end
end

但是我收到一个错误:

private method `open' called

在 zip.open 通话中。

如何在本地保存 zip 以进行处理?

4

2 回答 2

12

作为 Rails 5.2 中的替代方案,您可以这样做:

def process_zip      
   # Download the zip file in temp dir
   zip_path = "#{Dir.tmpdir}/#{zip.filename}"
   File.open(zip_path, 'wb') do |file|
       file.write(zip.download)
   end   

   Zip::File.open(zip_path) do |zip_file|  
       # process the zip file
       # ...
       puts "processing file #{zip_file}"
   end
end
于 2018-06-13T12:56:58.403 回答
1

这是一个边缘指南(edgeguides.rubyonrails.orgURL 中的注释);它适用于 GitHub 上rails/rails存储库的主分支。master 的最新变化还没有包含在 Rails 的发布版本中。

您可能正在使用 Rails 5.2。使用边缘 Rails 来利用ActiveStorage::Blob#open

gem "rails", github: "rails/rails"
于 2018-05-25T20:23:12.860 回答