【问题标题】:NameError (uninitialized constant Unzipper::Zip) but only on Heroku deploy (Rails)NameError(未初始化的常量 Unzipper::Zip)但仅在 Heroku 部署(Rails)上
【发布时间】:2015-12-19 15:33:23
【问题描述】:

我有一个类 unzipper.rb 使用 Rubyzip 解压缩文件。

在我的本地环境中,我可以成功解压缩文件,而无需使用require 'zip' 明确包含依赖项

但在 Heroku 上,我得到了一个 NameError (uninitialized constant Unzipper::Zip),我只能通过使用显式 require 来解决它

问题:为什么在 Heroku 环境中需要这样做,而在 localhost 中不需要?我的印象是 Rails 自动需要所有 gem。

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 输出包括:

remote:        Using rubyzip 1.1.7

我确认两者都使用相同版本的 Ruby。

Rubyzip 没有初始化程序或环境配置。

宝石文件

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'

【问题讨论】:

  • 可能是环境问题。尝试将您本地的RAILS_ENV 设置为production,看看是否会发生同样的问题。
  • 您是否通过bundle exec 运行您的脚本?
  • @brito 我不在办公室,但会试试看!
  • @Alexey 这是从 rails 控制器调用的,我假设 heroku 使用 bundle exec 运行 rails?
  • 你能分享错误的堆栈跟踪吗?生产环境和开发环境之间的最大区别在于,默认情况下,文件在生产环境中被急切地加载。 rubyzip 的主文件名与其 gem 名不同,因此您可能必须在 Gemfile 中明确指定它:gem 'rubyzip', require: 'zip'

标签: ruby-on-rails ruby heroku ruby-on-rails-4.2


【解决方案1】:

Rubyzip 的主文件名与其 gem 名不同,因此您可能必须在 Gemfile 中明确指定它:

gem 'rubyzip', require: 'zip'

【讨论】:

  • 不错!这对我有用。我觉得我永远也想不通。高五@fivedigit!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 2012-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
相关资源
最近更新 更多