ruby on rails roo gem cannot load zip/zipfilesystem - ruby-on-rails

I am attempting to use the roo gem to process .xlsx spreadsheets that are uploaded by an outside party. I'm getting the following error:
LoadError (cannot load such file -- zip/zipfilesystem):
I've found a lot of questions similar to this one (such as cannot load such file -- zip/zip) and I've tried to follow their solutions. So far, to no avail.
I originally required 'roo' in the controller, and after getting this error tried requiring 'zip/zip', 'zip/zipfilesystem', and just 'zip'. None of these seem to fix anything. I've also tried adding :require => 'zip', :require => 'zip/zipfilesystem', :require => 'zip/zip' to the Gemfile, and none of that seemed to change anything. Here is some pertinent code:
in Gemfile:
# for spreadsheet upload management
gem 'roo'
gem 'rubyzip'
gem 'spreadsheet'
gem 'nokogiri'
installed versions:
nokogiri (1.6.0)
roo (1.12.1)
rubyzip (1.0.0)
spreadsheet (0.8.9)
in Controller:
require 'roo'
module BatchOrderProcessing
class DataFilesController < ApplicationController
def create
# some code here ...
when ".xlsx"
spreadsheet = Roo::Excelx.new(uploaded_io.path, nil, :ignore)
header = spreadsheet.row(1)
if # some validation stuff...
puts "spreadsheet format inappropriate"
redirect_to # some place
end
process_datafile(fname, spreadsheet)
# more code ...
end
private
def process_datafile(fname, spreadsheet)
#df = DataFile.new
#df[:filename] = ActiveRecord::Base.connection.quote(fname)
if #df.save
begin
# parse asynchronously
datafile_scheduler = Rufus::Scheduler.new
datafile_scheduler.in '3s' do
#df.process_spreadsheet(spreadsheet)
end
redirect_to #df
rescue => e
# more code ...
end
else
# more code ...
end
end
I think this thing is crapping out before it gets to the model (where the process_spreadsheet() code is), but just in case, here's some model code:
def process_spreadsheet(spreadsheet)
# do some stuff
puts "parsing spreadsheet"
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
row_array << row
invoice << row.to_s
# some more code....
dfi = DataFileItem.new()
dfi.attributes = row.to_hash.slice(*accessible_attributes)
dfi.data_file_id = self.id
dfi.save
self.data_file_items << dfi
# Update stuff in our DB based on rows in row_array...
end
I'm using rails 3.2.13 and ruby 2.0.0p195.
Am I requiring the wrong thing (or in the wrong way) somewhere? Let me know if any other code snippets would be helpful. Thaaaaanks.

rubyzip v1.0.0 was released 29 August 2013: https://github.com/rubyzip/rubyzip/releases
This is a new major version number, and more than one gem or project that depends on this has been caught out by the break with backwards-compatibility.
The quickest "get my code working like before" fix is to alter Gemfile reference to rubyzip:
gem 'rubyzip', '< 1.0.0'
In the longer-term, this may not be the best fix, it depends on how and/or why you are using rubyzip. I expect some gem publishers such as roo's authors will need to figure out how to transition nicely so that their own users don't end up with simultaneous requirements for incompatible versions of rubyzip.
Just opinion:
Seeing this in action has actually made me much less a fan of Ruby gems semantic versioning for major versions. If I ever break with backwards compatibility on any of my own projects, I think I'll just start a new gem, and put a notice on the old gem.

Try adding the zip-zip gem to your project. It provides a simple adapter for your dependencies using the RubyZip v0.9.9 interface allowing you to upgrade to RubyZip v1.0.0.

This has been fixed in the roo gem. You need to update to version 1.12.2 or higher to fix. See the issue here: https://github.com/Empact/roo/pull/65

Related

NameError - uninitialized constant Zip

I am trying to unzip a file in my Spree plugin.
Defined the unzipping method in a module which looks like this.
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
I have included the rubyzip gem in my Gemfile.
gem 'rubyzip'
gem 'zip-zip'
When trying to run it, I am getting the following error.
NameError - uninitialized constant ImportImages::Zipper::Zip:
I have tried every solution provided in stackoverflow and other sites. I tried downgrading the version of rubyzip which is 1.2.0 now and add require 'zip' or require 'zip/zip'. Both returned load error.
I have try adding require 'zip/filesystem' to the class. But got
LoadError - cannot load such file -- zip/zipfilesystem
Any solution for this?
Include rubyzip in gem file in this way:
gem 'rubyzip', require: 'zip'
See this question
It's looking for a nested Constant. Change line Zip::File.open(zip) do |zip_file| with below:
::Zip::File.open(zip) do |zip_file|
It should work.
Also make sure you require rubygem/bundle setup. Though in spree it should've already been done.
Babar's answer is correct, but you also need to add require 'zip' in application_controller.rb

Whois gem not working in rails

class DomaincheckerController < ApplicationController
def index
end
def store
r =Whois.whois(secure_params['domain'])
render :text => "#{r}"
end
private
def secure_params
params.require(:whois).permit(:domain)
end
end
This is my domainchecker controller. The index method renders a form. After submitting the form it goes to store method. Here I am trying to use the whois gem. I have installed whois gem by running gem install whois. But I am getting this error.
uninitialized constant DomaincheckerController::Whois
The problem is that you installed the gem directly and not using bundler, therefore the Rails app can't find the dependency.
In order to install a gem in a Rails project you need to edit the Gemfile file and add the gem there. Once added, run
$ bundle
in order to install the dependency. Check the documentation about the Gemfile.

Using http_accept_language gem on rails 3

I'm trying to use this gem to determine the user's preferred language, and running into some trouble.
undefined local variable or method http_accept_language for #<HomeController:0x964f5ec>
I included the gem in the Gemfile, ran bundle install, and restarted the server multiple times. Why doesn't my app recognize the gem?
Also, in my ApplicationController I wrote the following method:
def set_i18n_locale
http_accept_language.user_preferred_languages
available = %w{en kr}
params[:locale] = http_accept_language.preferred_language_from(available)
if params[:locale]
I18n.locale = params[:locale]
end
end
One thing I don't understand is the second line,
http_accept_language.user_preferred_languages
From https://github.com/iain/http_accept_language, this is supposed to return a sorted array. I thought I had to store the array into some variable and use it, but the author just throws the method like that. How does that work? Can't I just do the following?
available = %w{en kr}
params[:locale] = http_accept_language.language_region_compatible_from(available)
I am just a little confused by the author's explanation.
Thank you for your help.
UPDATE:
the gem, http_accept_language, doesn't seem to be installed successfully. It's on the gem list, but when I try to uninstall it, the error message shows that it's not installed. Why does this happen?
max#max-VirtualBox:~/appe$ gem list
*** LOCAL GEMS ***
...
http_accept_language (1.0.2)
...
max#max-VirtualBox:~/app$ sudo gem uninstall http_accept_language
INFO: gem "http_accept_language" is not installed
Try using request.user_preferred_languages instead of http_accept_language.user_preferred_languages.
In the documentation states that since version 2.0, the gem is a Rack middleware, but the problem is that the only 2.0 version released in June 2012 is only a pre-release. Therefore, to get the version 2.0 you need to do this:
gem 'http_accept_language', '~> 2.0.0.pre'
I didn't have to use the gem to implement the feature I wanted.
def set_i18n_locale
unless params[:locale]
params[:locale] = extract_locale_from_accept_language_header
end
available = ['en', 'kr']
if available.include? params[:locale]
I18n.locale = params[:locale]
end
end
def extract_locale_from_accept_language_header
request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
end
def default_url_options
{ :locale => I18n.locale }
end
Official Guide and Agile Development helped a lot.
As for me solution was to apply #DouweM PR from the github. Here is the line from the Gemfile:
gem 'http_accept_language', :git => 'https://github.com/DouweM/http_accept_language', :branch => 'no-middleware-no-crash'
put in your Gemfile:
gem 'http_accept_language', '~> 2.0.0.pre'
and then in the code use env aka:
env.http_accept_language
works for me.
% bundle show|grep acc
* http_accept_language (2.0.0.pre)

'no such file to load -- net/ssh' from rails Controller on Ubuntu

I have a very simple controller:
require 'net/ssh'
class MyController < ApplicationController
def foo
render :text => 'bar'
end
end
But when I request http://server:3000/my/foo I get:
MissingSourceFile in MyController#foo
no such file to load -- net/ssh
The gem is installed
> gem list net-ssh
*** LOCAL GEMS ***
net-ssh (2.0.11)
Also, I tried require 'net/ssh' in IRB, and it works.
MyController works fine on Windows, but fail on Ubuntu.
What can be wrong?
In a project I am working on we have used the config/environment.rb file to hold the gem require stuff. So
Rails::Initializer.run do |config|
# ...
config.gem 'net-ssh'
config.gem 'daemons'
config.gem 'slave'
config.gem 'vpim'
config.gem 'json'
# ...
end
I think you will require 'net-ssh' rather than 'net/ssh'. However we did run into a problem where have a hyphen in the name of the gem led to failures. Then we had to do
config.gem 'Ruby-IRC', :lib => 'IRC'
so that version maybe required for you. So that would be
config.gem 'net-ssh', :lib => 'net/ssh'
in case of rails 3.0
this solution if OK.
add this in the yourapp/Gemfile,
gem 'net-ssh
This may help:
Rails Gem Dependencies and Plugin Errors
This is also worth watching:
Railscasts: Gem Dependencies
In my case, since it's a stand alone ruby app, I only needed to require rubygems.
You can also use Dr Nic's ''gemsonrails'' and load vendored gems as plugins, check:
http://gemsonrails.rubyforge.org
I think, the original problem was that I used normal user instead of root:
$ gem install net-ssh
WARNING: Installing to ~/.gem since /usr/lib/ruby/gems/1.8 and
/usr/bin aren't both writable.
WARNING: You don't have /home/alex/.gem/ruby/1.8/bin in your PATH,
gem executables will not run.
So, I guess, rails could not find this gem.

Why is this line breaking Rails with Passenger on DreamHost?

Ok, so I have a Rails app set up on DreamHost and I had it working a while ago and now it's broken. I don't know a lot about deployment environments or anything like that so please forgive my ignorance. Anyway, it looks like the app is crashing at this line in config/environment.rb:
require File.join(File.dirname(__FILE__), 'boot')
config/boot.rb is pretty much normal, but I'll include it here anyway.
# Don't change this file!
# Configure your app in config/environment.rb and config/environments/*.rb
RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
module Rails
class << self
def boot!
unless booted?
preinitialize
pick_boot.run
end
end
def booted?
defined? Rails::Initializer
end
def pick_boot
(vendor_rails? ? VendorBoot : GemBoot).new
end
def vendor_rails?
File.exist?("#{RAILS_ROOT}/vendor/rails")
end
def preinitialize
load(preinitializer_path) if File.exist?(preinitializer_path)
end
def preinitializer_path
"#{RAILS_ROOT}/config/preinitializer.rb"
end
end
class Boot
def run
load_initializer
Rails::Initializer.run(:set_load_path)
end
end
class VendorBoot < Boot
def load_initializer
require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
Rails::Initializer.run(:install_gem_spec_stubs)
end
end
class GemBoot < Boot
def load_initializer
self.class.load_rubygems
load_rails_gem
require 'initializer'
end
def load_rails_gem
if version = self.class.gem_version
gem 'rails', version
else
gem 'rails'
end
rescue Gem::LoadError => load_error
$stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
exit 1
end
class << self
def rubygems_version
Gem::RubyGemsVersion if defined? Gem::RubyGemsVersion
end
def gem_version
if defined? RAILS_GEM_VERSION
RAILS_GEM_VERSION
elsif ENV.include?('RAILS_GEM_VERSION')
ENV['RAILS_GEM_VERSION']
else
parse_gem_version(read_environment_rb)
end
end
def load_rubygems
require 'rubygems'
min_version = '1.1.1'
unless rubygems_version >= min_version
$stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
exit 1
end
rescue LoadError
$stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
exit 1
end
def parse_gem_version(text)
$1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
end
private
def read_environment_rb
File.read("#{RAILS_ROOT}/config/environment.rb")
end
end
end
end
# All that for this:
Rails.boot!
Does anyone have any ideas? I am not getting any errors in the log or on the page.
-fREW
I had the same problem on DreamHost. Freezing rails and unpacking all gems got me past it.
rake rails:freeze:gems
rake gems:unpack:dependencies
My guess would be that you're breaking because of a newer version of the Rails gems on Dreamhost. At least, that's been my issue when things have blown up in something like boot.rb.
Try freezing the gems from your development environment into your vendor/rails directory.
Ya - the problem is not really in boot.rb - it's just that boot.rb is where rails is actually loaded.
So you'll get an error like this if you've specified a version of Rails that just doesn't exist on your dreamhost slice. This can happen if you either upgrade your project, start a new project (and forget that you upgraded rails in the meanwhile) or if you're still using an old version of rails and it's now been removed from the dreamhost server that you're on.
To figure out which is is, look in config/environment.rb for the line that'll read something like:
RAILS_GEM_VERSION = '2.3.4' unless defined? RAILS_GEM_VERSION
Then ssh to your dreamhost server and type gem list and see if your version is in the list.
If not, you an try several options. Lets say the version you're using is 2.3.4
To begin with, try: gem install rails -v=2.3.4 then restart. That may be all that is required.
If that doesn't work, then try freezing and unpacking the gems (as per the other answer here).
There is also another possibility - that you're actually missing a gem that rails depends on but which is failing silently - eg a dependency on a certain version of rack caught me out once. But you may also have other gem dependencies
If you run rake gems you will be able to list all the gems that your project knows about that it needs - make sure they're installed to begin with.
Then, as a sort of rough smoke-test, try running script/console - if you're missing an important rails gem, script/console won't load and should fail, giving you a notice about the gem you need.
Update:
If you're trying to run v 2.3.5, you may also be suffering from this problem:
Bypassing rack version error using Rails 2.3.5
In which case you'll need to follow the instructions there.

Resources