问题描述
我有一个使用Rubyzip解压缩文件的类 unzipper.rb 。
在我的本地环境中,我可以使用 require'zip'
在不显示包括依赖项的情况下成功解压缩文件。一个 NameError(未初始化的常量Unzipper :: Zip),我只能通过使用 require
问题:为什么在Heroku环境中这样做是必要的,但在本地主机上不行?
app / services / unzipper.rb
需要'zip'#只有Heroku需要。无本地工作!
类解压缩
OVERRIDE_FILES =真
def初始化(文件)
@file =文件
结束
def self.unzip(文件和块)
Unzipper.new(文件).unzip(和块)
结尾
def解压缩
open_zip do | zip |
产量extract_files(zip)
结束
结束
私人
def open_zip(& block)
:: Zip :: File.open(@ file.path,&block)
end
def extract_files(zip)
files = []
zip.each do |进入|
path =#{rails_temp_directory} /#{entry.name}
entry.extract(path){OVERRIDE_FILES}
files<<路径
结束
文件
结束
def rails_temp_directory
#{Rails.root} / tmp
结束
结束
运行bundler时的Heroku输出包括:
remote:使用rubyzip 1.1.7
使用相同版本的Ruby。
没有用于Rubyzip的初始化程序或环境配置。
Gemfile
source'https://rubygems.org'
gem'rails ','4.2.0'
gem'pg',group::production
gem'sass-rails','〜> 5.0'
gem'uglifier','> = 1.3.0'
gem'coffee-rails','〜> 4.1.0'
gem'jquery-rails'
gem'turbolinks'
gem'jbuilder','〜> 2.0'
gem'sdoc','〜> 0.4.0',组::doc
gem'slim-rails'
gem'paperclip'
gem'rest-client'
gem'bootstrap-sass','〜> 3.3.3'
gem'validates_serialized'
gem'puma'
gemnokogiri
gemcocoon
gem'sidekiq'
gem'sinatra',:require => nil#对于sidekiq网页监控
gem'aws-sdk','< 2.0'
gem'rails_12factor',group::production
gem'rubyzip'
group:development,:test do
gem'byebug'
gem'web-console','〜> 2.0'
gem'spring'
gem'sqlite3'
gem'shoulda-matchers'
gem'rspec-rails'
gem'factory_girl_rails'
gem'capybara'
gem'selenium-webdriver'
gem'database_cleaner'
gem'guard-rspec'
gem' faker'
end
group:test do
gem'webmock'
end
ruby'2.2.0'
Rubyzip的主文件名与其宝石名称不同,在Gemfile中显式指定它:
gem'rubyzip',要求:'zip'
I have a class unzipper.rb that unzips a file using Rubyzip.
In my local environment, I can succesfully unzip a file without explictly including the dependency using require 'zip'
On Heroku though, I get an NameError (uninitialized constant Unzipper::Zip) which I could only resolve by using the explict require
Question: Why would this be necessary in the Heroku environment, but not on localhost? I was under the impression that Rails required all gems automatically.
app/services/unzipper.rb
require 'zip' # Only required for Heroku. Works locally without! class Unzipper OVERRIDE_FILES = true def initialize(file) @file = file end def self.unzip(file, &block) Unzipper.new(file).unzip(&block) end def unzip open_zip do |zip| yield extract_files(zip) end end private def open_zip(&block) ::Zip::File.open(@file.path, &block) end def extract_files(zip) files = [] zip.each do |entry| path = "#{rails_temp_directory}/#{entry.name}" entry.extract(path) { OVERRIDE_FILES } files << path end files end def rails_temp_directory "#{Rails.root}/tmp" end end
Heroku output when running bundler includes:
remote: Using rubyzip 1.1.7
I verified both are using the same version of Ruby.
There is no initializer or environment config for Rubyzip.
Gemfile
source 'https://rubygems.org' gem 'rails', '4.2.0' gem 'pg', group: :production gem 'sass-rails', '~> 5.0' gem 'uglifier', '>= 1.3.0' gem 'coffee-rails', '~> 4.1.0' gem 'jquery-rails' gem 'turbolinks' gem 'jbuilder', '~> 2.0' gem 'sdoc', '~> 0.4.0', group: :doc gem 'slim-rails' gem 'paperclip' gem 'rest-client' gem 'bootstrap-sass', '~> 3.3.3' gem 'validates_serialized' gem 'puma' gem "nokogiri" gem "cocoon" gem 'sidekiq' gem 'sinatra', :require => nil # For sidekiq web monitoring gem 'aws-sdk', '< 2.0' gem 'rails_12factor', group: :production gem 'rubyzip' group :development, :test do gem 'byebug' gem 'web-console', '~> 2.0' gem 'spring' gem 'sqlite3' gem 'shoulda-matchers' gem 'rspec-rails' gem 'factory_girl_rails' gem 'capybara' gem 'selenium-webdriver' gem 'database_cleaner' gem 'guard-rspec' gem 'faker' end group :test do gem 'webmock' end ruby '2.2.0'
Rubyzip's main file name is different than its gem name, so you might have to specify it explicitly in the Gemfile:
gem 'rubyzip', require: 'zip'
这篇关于NameError(未初始化的常量Unzipper :: Zip),但只在Heroku部署(Rails)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!