[英]Download an active Storage attachment to disc
The guide says that I can save an attachment to disc to run a process on it like this: 该指南说我可以保存光盘附件以在其上运行一个过程,如下所示:
message.video.open do |file|
system '/path/to/virus/scanner', file.path
# ...
end
My model has an attachment defined as: 我的模型的附件定义为:
has_one_attached :zip
And then in the model I have defined: 然后在我定义的模型中:
def process_zip
zip.open do |file|
# process the zip file
end
end
However I am getting an error : 但是我收到一个错误:
private method `open' called
on the zip.open call. 在zip.open调用。
How can I save the zip locally for processing? 如何在本地保存zip以进行处理?
As an alternative in Rails 5.2 you can do this: 作为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
That's an edge guide (note edgeguides.rubyonrails.org
in the URL); 这是一个边缘指南(注意
edgeguides.rubyonrails.org
中的edgeguides.rubyonrails.org
); it applies to the master branch of the rails/rails repository on GitHub. 它适用于GitHub上rails / rails存储库的master分支。 The latest changes in master haven't been included in a released version of Rails yet.
主要的最新更改尚未包含在已发布的Rails版本中。
You're likely using Rails 5.2. 您可能正在使用Rails 5.2。 Use edge Rails to take advantage of
ActiveStorage::Blob#open
: 使用edge Rails来利用
ActiveStorage::Blob#open
:
gem "rails", github: "rails/rails"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.