Rails: LoadError - Cannot load such file (requiring a gem) - ruby-on-rails

I'm import the active_campaign gem into a controller like so (already included in my Gemfile and ran bundle install):
require 'active_campaign'
class Website::MyController < ApplicationController
def create
client = ::ActiveCampaign::Client.new("url","api-key")
# ...
end
end
I get the following error:
LoadError in Website::MyController#create
cannot load such file -- active_campaign
Removing the require 'active_campaign' line
After removing the require line, I now get:
NameError in Website::MyController#create uninitialized constant ActiveCampaign
How can I get this to work?

It is rails controller so you don't have explicit require any gems. Bundler does it.
Perhaps you can't access ActiveCampaign constant because you added gem after starting a server (so after bundler require all gems and give you access to their classes).
Ensure you do following steps:
Kill server
Run bundle update or bundle install
Run server again
Now bundler should give you access to all active_campaign's classes in rails controllers

Related

Installed gem can't load from rails controller

I am dabbling with ruby on rails for the first time and have run into an issue while trying to load a gem from a controller.
To install the gem, I added it to (the top just below source) the Gemfile and ran bundle install. The gem is shown as installed and is in the Gemfile.lock. When installing, I was prompted for sudo password and a bundle show <gem> shows it in a system path (/var/lib/gems).
In the controller (in app/controllers/my_controller.rb), I am requiring it as follows:
class MyController < ApplicationController
def index
#somevar = dostuff
end
private
def dostuff
require 'some/lib'
return "foo"
end
end
When I access the app, I get the following error:
cannot load such file -- some/lib
If i put the dostuff function into dostuff.rb in the project root and execute it with ruby dostuff.rb the gem is loaded fine.
Am I missing something about how gems are loaded in a rails environment or am I loading it up incorrectly?
I have read through these similar questions but they have not resolved the problem:
Rails can't find installed gem - is rbenv the only solution? would like to try get this working with a system gem.
Does Rails load all installed gems? - im explicitly loading it but failing

Simple error I think, but how can I require a gem installed file in one of my controllers? I get a LoadError

I installed the venice gem, which can be found here. https://github.com/nomad/venice
After installing the gemfile, it suggests using
require 'venice'
I have done this in my /api/v1/purchases_controller.rb class. Like this...
require 'venice'
class Api::V1::PurchasesController < ApplicationController
However, I end up getting the following error
FATAL -- :
LoadError (cannot load such file -- venice):
app/controllers/api/v1/purchases_controller.rb:1:in `<top (required)>'
Why is this. How can I use require this needed file in this one controller class (the only class I need it in).
In Rails applications gems are usually handled with Bundler.
Add the following line to your applications' Gemfile:
gem 'venice'
Then run bundle install in your console and restart your application.

Why am I getting LoadError or NameError when trying to use HTTParty?

I'm trying to use the HTTParty gem as a part of the gem that I am creating; however I keep getting an uninitialized constant NameError or 'require': cannot load such file -- httparty LoadError. My class starts like this:
module Reporting
class GitlabIssue
include HTTParty
...
Leaving this at it is, I receive the following error:
uninitialized constant Reporting::GitlabIssue::HTTParty (NameError)
Various searches turned up the solution for this is to require 'httparty' before the class block is opened. So I put the following at the top of my file:
require "httparty"
With that in place, I receive this error:
'require': cannot load such file -- httparty (LoadError)
My gemspec has the seemingly correct command to use the gem:
spec.add_dependency "httparty"
and when I do a bundle update/install, it lists the httparty gem as being installed and at version 0.13.3
When I load up an irb session, I can successfully require 'httparty' and it returns true without any errors. Any ideas what could be going on here?
Some system info: I'm running ruby 2.1.0p0 [x86_64-linux] and Rails 4.1.1
I think this has to do with the order that your files (and HTTParty) are loaded in your gem. If you have the standard directory layout for your gem try this:
In your_app.gemspec ->
spec.add_dependency 'httparty'
At the top of your_app/lib/your_app.rb ->
require 'httparty'
I just runned in the exact same issue here.
Turns out I included HTTPParty instead of HTTParty

Rails/Ruby/Postgres - LoadError cannot load such file -- pg_ext

I am trying to call a Ruby script (which connects to a postgres db) using the rails controller below, however it appears it is having problems loading one of the PG gem files. I have set my require statement to require 'pg' and tried the absolute path as well (require /usr/local/rvm/gems/ruby-1.9.3-p194#railsTest/gems/pg-0.14.0/lib/pg/). The file 'pg_ext' is in fact present in the directory. Additionally, I can run the ruby script standalone without any problems (dbrubyscript.rb), however when rails is added to this equation it craps out with a cannot load such file -- pg_ext error.
Any direction here would be much appreciated as I have not been able to find anything online that fixes this issue
Rails controller:
class TestdlController < ApplicationController
def runmyscript
load "/usr/local/rvm/my_app/ruby_scripts/reports/dbrubyscript.rb"
send_file '/usr/local/rvm/tmp/failedtests.csv', :type => 'text/csv', :disposition => 'inline'
flash[:notice] = "Reports are being processed..."
end
end
.rb file (dbrubyscript.rb) has the following:
require 'rubygems'
require 'pg'
connects to (production) database
#conn = PGconn.connect("zzzzz.test.prod", 5432,"","","yyyyy_prod" ,"postgres", "xxxxxx")
.....
Trace Error:
LoadError in TestdlController#runmyscript
cannot load such file -- pg_ext
Rails.root: /usr/local/rvm/my_app Application Trace | Framework Trace
| Full Trace app/controllers/Testdl_controller.rb:3:in `runmyscript'
This error occurred while loading the following files:
/usr/local/rvm/my_app/ruby_scripts/reports/dbrubyscript.rb
/usr/local/rvm/gems/ruby-1.9.3-p194#railsTest/gems/pg-0.14.0/lib/pg/
pg_ext
Try running ruby /usr/local/rvm/gems/ruby-1.9.3-p194#railsTest/gems/pg-0.14.0/lib/pg/ext/extconf.rb and see what errors you get. That helped me determine that in my case, my PostgreSQL client was too old. Your error may be different since you appear to have a current-ish versioninstalled.
I had the same problem.
I have a stand alone ruby script. It connects via pg to postgres and worked if I ran it directly from the shell.
If I tried to run it via rspec I get the Error cannot load such file -- pg.
Solved: The problem for rspec was, the pg gem was not defined in the Gemfile. After put pg into Gemfile, and retestet via rspec, it worked.
Try adding a line to your Gemfile:
gem "pg"
Then run bundler via the command line:
bundle install
Rails uses Bundler to manage your gems and dependencies. You can read a bit more about the idea behind Bundler here: http://gembundler.com/v1.2/rationale.html

Bundle doesn't import gnuplot gem correctly; NameError: uninitialized constant thrown on access

I have a module that lives in my Rails app as lib/render_graphs.rb. It looks like:
require 'gnuplot'
module RenderGraphs
def render_standard_curve_graph(standard_curve, params, term = nil, output = nil)
Gnuplot.open do |gp|
# do things
end
end
end
if __FILE__ == $0
include RenderGraphs
render_standard_curve_graph(...)
end
If I invoke it on the command line with ruby -r rubygems render_graphs.rb, it works just fine. But if I try to call render_standard_curve_graph from my Rails app (I call it from a controller, which includes RenderGraphs) or from the Rails console, it gives an error like:
NameError: uninitialized constant RenderGraphs::Gnuplot
from .../lib/render_graphs.rb:31:in `render_standard_curve_graph'
from (irb):32
If I run require '/Library/Ruby/Gems/1.8/gems/gnuplot-2.3.6/lib/gnuplot.rb' at the Rails console before I call render_standard_curve_graph, it works just fine. What's wrong with gem 'gnuplot' in my Gemfile? bundle show gnuplot yields
/Library/Ruby/Gems/1.8/gems/gnuplot-2.3.6...
I am baffled. gnuplot is in my Gemfile and I've run bundle install and restarted the console (several times). Similar calls to other gems (GSL and roo) work just fine from other modules in my lib directory. What am I missing?
It's a bug in the GSL gem. The GSL gem must be loaded after the gnuplot gem, or else it prevents the Gnuplot module from appearing to the interpreter. Bundler does not support requiring gems in a particular order, so the fix that does not require modifying the gsl gem is to edit config/boot.rb and explicitly require 'gnuplot' immediately after require 'rubygems', before Bundler is initialized.
You can try to be explicit and always grab Gnuplot constant from the top level.
(When you run it on the command line, you are including RenderGraphs into top level,
and render_standard_curve_graph is able to look up Gnuplot at that level.)
module RenderGraphs
def render_standard_curve_graph(standard_curve, params, term = nil, output = nil)
::Gnuplot.open do |gp|
# do things
end
end
end
Sigh. Data science is so hard.
I had code like
require "gsl"
# prepare data
# ...
require "gnuplot"
# plot data
# ...
And I was getting NameError: uninitialized constant Gnuplot
I Turned it into
require "gnuplot"
require "gsl"
# prepare data
# ...
# plot data
# ...
And the error was gone.

Resources