No such file to load -- 'gem name' using require - ruby-on-rails

I am using gem differ https://github.com/pvande/differ
I have a helper
require 'differ'
module AnswersHelper
def self.getDiff (text1, text2)
Differ.format = :html
diff = Differ.diff_by_word(#current, #original)
end
end
But I get an error No such file to load -- differ
If I remove require line
I get an error at that line
Differ.format = :html
uninitialized constant QuestionsController::Differ
When I tried following commands in rails console it worked
require 'differ'
diff = Differ.diff_by_word("text1","text2)
I have gem differ in my gemfile
and also I tried
require_relative 'differ'
and
require './differ'
UPD: seems restarting server helps, I'll check it right now

Restarting server helped........

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

Gem not initializing within my app

I have come across an issue with my gem, it seems that it is not initializing when starting the rails server, if i run this script in the console i can access the model classes, however when i start the rails server i get 'uninitialized constant errors'
require "blogModels/version"
module BlogModels
Gem.find_files("models/*.rb").each do |f|
filename = File.basename(f, '.*')
class_name_symbol = filename.classify.to_sym
autoload class_name_symbol, "models/#{filename}"
end
end
my gemfile
gem 'mygem', :git => "pathtogem"
afterwhich i ran bundle
Would there be a reason as to why it is not loading when i am retrieving it from git?
Thanks

delayed_job gives "NameError: uninitialized constant"

I'm trying to move a current working task (in production and in the console) to use delayed_job in a Rails 2 app but keep getting the error:
ThermalImageJob failed with NameError: uninitialized constant Barby::Code128B
I've pored through others' code searching for an answer to no avail. Here's my code:
/lib/thermal_image_job.rb
class ThermalImageJob < Struct.new(:order_id)
def perform
order = Order.find(order_id)
order.tickets.each do |ticket|
ticket.barcodes.each do |barcode|
barcode.generate_thermal_image
end
end
end
end
/app/controllers/orders_controller.rb
Delayed::Job.enqueue(ThermalImageJob.new(#order.id))
/app/models/barcode.rb
def generate_thermal_image(format=:gif)
filename = "#{barcode}_thermal.#{format}"
temp_file_path = File.join("#{RAILS_ROOT}", 'tmp', filename)
unless FileTest.exists?(temp_file_path)
barcode_file = File.new(temp_file_path, 'w')
code = Barby::Code128B.new(barcode)
....
end
Gemfile
gem "delayed_job", "2.0.7"
gem "daemons", "1.0.10"
Well, after much head banging, I figured it out, so I'm posting this to help the next person. The problem was that it couldn't find the barby libs, so I added a require at the beginning of my class:
require "barby/outputter/rmagick_outputter"
require "barby/barcode/code_128"

Rack Error -- LoadError: cannot load such file

Trying to go through the tekpub rack tutorial but run into this error.
Boot Error
Something went wrong while loading app.ru
LoadError: cannot load such file -- haiku
There is a file named haiku.rb in the same directory as the app I am trying to run but I get the above error while trying to run the program. Here is the code:
class EnvironmentOutput
def initialize(app=nil)
#app = app
end
def call(env)
out = ""
unless(#app.nil?)
response = #app.call(env)[2]
out+=response
end
env.keys.each {|key| out+="<li>#{key}=#{env[key]}</li>"}
["200",{"Content-Type" => "text/html"},[out]]
end
end
require 'haml'
require 'haiku'
class MyApp
def call(env)
poem = Haiku.new.random
template = File.open("views/index.haml").read
engine = Haml::Engine.new(template)
out = engine.render(Object.new, :poem => poem)
["200",{"Content-Type" => "text/html"}, out]
end
end
use EnvironmentOutput
run MyApp.new
I'm sure its a small error as the code is the same as in the tutorial and it works for him...
Thanks
You'll want to read up on ruby load path (either $LOAD_PATH or $:). By default, ruby has a load path which includes wherever your gems are installed, which is why you can do require 'haml' without providing the full path to where your haml gem is located.
When you type require 'haiku', you're basically telling ruby to look for some file called haiku.rb somewhere in it's load path, and the LoadError comes from ruby not finding your haiku.rb file in any of the directories listed in $LOAD_PATH (or $:, which is just shorthand for $LOAD_PATH).
You can solve this in one of (at least) two ways:
change require 'haiku' to require File.dirname(__FILE__) + '/haiku.rb' to explicitly tell ruby what file to load
add the current working directory to your load path: $:.push(File.dirname(__FILE__)). This way you can keep the require 'haiku' part.

no such file to load -- RMagick.rb Rails, Ubuntu

I have ruby and my gems installed in my home directory using rvm. 'require RMagick' works from the console and irb, but not from within my Rails app. I have the gem listed in my Gemfile.
require 'RMagick'
class PetsController < ApplicationController
def image
image = Image.new(image_path("base/Base.png"))
#image = Pet.composite(0, '4a4a4a')
#response.headers["Content-type"] = image.mime_type
render :text => image.to_blob
end
end
If I remove the require from my controller, I get
uninitialized constant PetsController::Image
I also have tried require 'RMagick' in boot.rb, and by referring to the Image object using Magick::Image (which gives me uninitialized constant PetsController::Magick.
What am I doing wrong?
Solved it! Restarting the server fixed it. I guess I hadn't restarted the server when I added rmagick to my gemfile.
Is it possible that you are running the rails app with another user than the one that has the RMagick gem installed?

Resources