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
Related
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
I'm trying to install the calendar_helper gem. I included the gem in my Gemfile:
gem 'calendar_helper'
I ran bundle install and loaded fine.
Using calendar_helper (0.2.4)
0.2.4 is the newest version in GitHub, so that looks good. I'm running on Pow so I don't need to restart the server (though I tried that anyway). Adding a call to the method calendar throws an error:
undefined method `calendar'
I feel like something could be wrong with my Rails installation or something. Any ideas?
It doesn't look like version 0.2.4 is up-to-date with what's on github; it's missing the subclass of Rails::Engine necessary to load the helper. The key line on the edge source is here: https://github.com/topfunky/calendar_helper/blob/master/lib/calendar_helper.rb#L231.
You may be able to fix this by installing the gem from edge:
gem 'calendar_helper', :git => 'git://github.com/topfunky/calendar_helper.git'
Edited:
If that still isn't working, you can also try this in your ApplicationHelper:
require 'calendar_helper'
module ApplicationHelper
include CalendarHelper
end
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
This is my first time trying to get a Gem to work in rails where I haven't just been able to follow the documentation.
I first installed the gem using sudo gem install ping back and then added it to my Gemfile via gem 'pingback'. I then ran bundle install and it shows it installed in the list it outputs.
So then I wrong a little function that looks like this and is in my posts controller:
def send_trackback(posts)
posts.each do |post|
source_uri = "http://example.com/posts/#{post.slug_url}"
target_uri = post.target_url
Pingback::Client.new.ping(source_uri, target_uri)
end
end
whenever I try to load the admin page that sends the trackbacks I get the following:
NameError in PostsController#pingback
uninitialized constant PostsController::Pingback
Do I have to do more than just install the gem via bundler and then plug and play?
Update
adding require 'pingback' to the top of my posts controller results in this:
cannot load such file -- pingback
The error message to me indicates that the VM is trying to find PingBack in PostsController, I am thinking you are missing a require or include statement for PingBack.
It may be a typo, but pingback needs to be one word, not 'ping back' for the line in the gemfile, and for the gem install.
I would try running 'bundle list' to make sure the gem is installed.
I restarted the rails server and I believe this has resolved this issue.
I've been trying to use HTTParty in my rails code
sudo gem install httparty
From the command line I can now successfully do
httparty "http://twitter.com/statuses/public_timeline.json"
When I try this in my rails app
require 'rubygems'
require 'httparty'
class FooController < ApplicationController
include HTTParty
def bar
blah = HTTParty.get("http://twitter.com/statuses/public_timeline.json")
end
end
I get the error message "no such file to load -- httparty"
I suspect there is something wrong with my environment?
You don't need to do 'include HTTParty' inside the Controller. Just remove that and it should work. I just tested it and it worked for me. If this doesn't work for you, you should add the gem to your environment.
Usually if you use a gem inside your Rails application, you should add the following to environment.rb:
config.gem "httparty"
The gem will be available in the application now and you don't need to add 'require' inside the Controller. Also, you don't need to require RubyGems inside a Controller.
When you use Rails 3, you need to put the following inside the Gemfile:
gem "httparty"
I hope it works for you. :)
The problem is, if you load a new gem, you have to restart the server even if you are in development.
I had this same error. I tried moving the require HTTParty all over, but found, all I needed to do was restart the rails server In the end I did not need to 'require HTTParty' nor 'include' it. It just needed to be loaded into rails.
1)include the httpary in your gemfile
open your gem file then add
gem 'httparty','YOUR VERSION NUMBER'
2) run bundle install in your command prompt of the app file
3) restart the server
Ran into the same problem. Then I switched from Ruby 1.8.7 to Ruby 1.9.2 and all errors varnished into thin air.
(Yes, it first took me quite some hours to come up with the possibility that the Ruby version might be the problem. Configured a secundairy server to avoid possible conflicts with 2 ruby versions, and after way to many hours I got my RoR stack up and running. And the first test with httparty (based on the example on top) worked out of the box! Finally can sleep RESTfully again :-)
I run into the same error whilst reviewing a project from a student, I change the name of the Gem from uppercase to lowercase then run bundle install. I then went ahead to change the format in which they were being imported from
require 'HTTParty' to require 'httparty' and boom it worked