![](/img/trans.png)
[英]NameError (uninitialized constant Unzipper::Zip) but only on Heroku deploy (Rails)
[英]NameError - uninitialized constant Zip
我試圖在Spree插件中解壓縮文件。
在如下所示的模塊中定義了解壓縮方法。
module ImportImages
class Zipper
def self.unzip(zip, unzip_dir, remove_after = false)
Zip::File.open(zip) do |zip_file|
zip_file.each do |f|
f_path=File.join(unzip_dir, f.name)
FileUtils.mkdir_p(File.dirname(f_path))
zip_file.extract(f, f_path) unless File.exist?(f_path)
end
end
FileUtils.rm(zip) if remove_after
end
end
end
我已將rubyzip gem包含在我的Gemfile中。
gem 'rubyzip'
gem 'zip-zip'
嘗試運行它時,出現以下錯誤。
NameError - uninitialized constant ImportImages::Zipper::Zip:
我已經嘗試了stackoverflow和其他站點中提供的所有解決方案。 我嘗試降級rubyzip的版本(現在是1.2.0),並添加require'zip require 'zip'
或require'zip require 'zip/zip'
。 兩者均返回加載錯誤。
我嘗試在類中添加require 'zip/filesystem'
。 但是得到了
LoadError - cannot load such file -- zip/zipfilesystem
有什么解決辦法嗎?
它正在尋找嵌套的常量。 更改Zip::File.open(zip) do |zip_file|
以下:
::Zip::File.open(zip) do |zip_file|
它應該工作。 還要確保您需要rubygem
/ bundle setup
。 盡管應該大禮包,但它應該已經完成了。
Babar的答案是正確的,但是您還需要在application_controller.rb中添加require 'zip'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.