rails Can not load a installed Gem - ruby-on-rails

I have installed the gem : oauth2
I can load it in irb:
$ irb -rubygems
irb(main):001:0> require 'oauth2'
=> true
I have :
require 'rubygems'
in my code
and have set RUBYOPT :
export RUBYOPT=rubygems
while I still get the error:
LoadError in XXXController#login
no such file to load -- oauth2
Any help will be appreciated.

Did you specify the gem as a dependency in Rails?
If you are using Rails 3, make sure the gem is listed in your Gemfile and you successfully executed the command $ bundle install.

Related

How do I get awesome_print to work without using ~/.irbrc file?

I want to use awesome print without putting it in my rails 5 app. Just in the console. The documentation for requiring it in irb is not working.
That's because bundler isolates the gems available to load to what's in your Gemfile.
The best way to get around this is to add the gem to your Gemfile
gem 'awesome_print', require: false, group: :development
And in your .irbrc, you can require it, so that it is only enabled for you:
begin
require 'awesome_print'
rescue LoadError => err
warn "could not require awesome_print: #{err}"
end
However, if you aren't permitted to add awesome_print to your repository for whatever reason, there are a few hacks to get gems installed, but not in your Gemfile to load in this GitHub Gist.
One such example that could be placed at the top of your .irbrc:
# Add all gems in the global gemset to the $LOAD_PATH so they can be used even
# in places like 'rails console'.
if defined?(::Bundler)
global_gemset = ENV['GEM_PATH'].split(':').grep(/ruby.*#global/).first
$LOAD_PATH.concat(Dir.glob("#{global_gemset}/gems/*/lib")) if
global_gemset
end
cd your/rails/project
irb
inside irb, run:
require 'awesome_print'
require './config/environment'
and you have both rails console and awesome_print gem while the gem is installed outside of the bundler.

"Cannot load such file" error when deploying Rails app to Heroku

I'm using a gem for gmail in my Rails app. My Gemfile contains:
gem 'gmail-api-ruby', :require => 'Gmail'
And in my controller, I initialize the gem with (using devise/omniauth to get the refresh_token from Google):
Gmail.client_id = ENV['CLIENT_ID']
Gmail.client_secret = ENV['CLIENT_SECRET']
Gmail.refresh_token = current_user.refresh_token
This works fine in development, but when I deploy to Heroku, I get the following error:
/app/vendor/bundle/ruby/2.2.0/gems/bundler-1.7.12/lib/bundler/runtime.rb:76:in `require': cannot load such file -- Gmail (LoadError)
I cannot figure out why. Should I be requiring the gem somewhere else in my app?
Ruby 2.2.0
Bundler: 1.8.5
Rails: 4.2.0
require is case sensitive if the underlying filesystem is case sensitive (which it is on linux, which is what underpins heroku)
Change to :require => 'gmail' instead of Gmail and you should be ok.
Just remove useless require option from your Gemfile because bundler can load classes inside of this gem without specify require option in your case.
gem 'gmail-api-ruby', '~> 0.0.10'
and run bundle install.
FYI: read section about require option here.

Gem can be required in IRB but not by Rails

When I add
require 'soundcloud'
in Rails and start the server using
rails server
I get
...rb:9:in `require': cannot load such file -- soundcloud (LoadError)
the same yields
=> true
in IRB.
I installed Ruby, Rails, etc following http://installrails.com/.
Any ideas what could be causing this?
Why don't you use Bundler?
Add to Gemfile:
gem 'soundcloud'
then run
bundle install
in the root of your project and it all should work automatically.

Require ruby gems in rails controller

require 'rubygems'
require 'mechanize'
agent = Mechanize.new
page = agent.get("http://google.com/")
This simple script works ok.
But if I'am trying to add
require 'rubygems' and require 'mechanize' to the Rails controller, server gives:
LoadError in NewsController#find
no such file to load -- mechanize
I use RVM on Ubuntu 10.04 server machnine. Ruby version: 1.9.2, Rails version: 3.0.3.
Server: Passanger under Apache2.
P.S. If I run rails server and go to mysite.com:3000 all works without any error, so there is a problem with Passanger!
Please, help me!
You shouldn't require gems in your controller. Thats why Bundler was added to Rails 3.
Just add mechanize to your Gemfile like this
gem "mechanize"
and run
bundle install
on the command line.
Any gem mentioned here will be required on application startup.
The way you manage dependencies in Rails 3, is using the Gemfile and Bundler.
Edit your Gemfile and add
gem "mechanize"
Then run
$ bundle install
Restart the server. The library will automatically be loaded. No need to manually require RubyGems.

Gem available in irb but not rails console

I am trying to use the RedCloth gem in my rails project. When I used irb I can load the gem:
require 'rubygems'
require 'RedCloth'
and it works fine, but when I try the same thing in the rails console I get an error message stating that the gem cannot be found.
Does anyone have any idea what might cause this?
You can append gem path to ruby load path. Do this:
gem which faker
=> /usr/local/ruby/......../faker-0.1.1/lib/faker.rb
Start Rails console and do the following:
$: << '/usr/local/ruby/......../faker-0.1.1/lib/'
and now load faker gem
require 'faker'
=> true
Does your rails project's Gemfile include gem 'RedCloth' in it? Rails will only load the gems specified in that file.

Resources