Require ruby gems in rails controller - ruby-on-rails

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.

Related

Ruby error: cannot load such file -- rest-client

I am using Ruby on Rails 4.
I am trying to
require 'rest-client'
in my controller so that I can parse the login information I am getting from a form and send it to an API.
I can verify that the gem is installed and is also in my Gemfile on the application root.
However, it is still throwing the "cannot load such file -- rest-client " when I try to require the file in my controller.
I have googled the error and most of the answers I saw were either the gem wasn't installed, wasn't in the Gemfile, or a combination of both those. Neither is the situation here.
Is my controller unable to access the rest-client gem for some reason? I have to use rest-client because it is required in the API.
This is the line I used to install the gem:
gem install rest-client
This is the homepage of the gem: https://github.com/archiloque/rest-client
Which just redirects you to https://github.com/rest-client/rest-client
I should also note that it works fine when I wasn't using the code in a Rails project but just running the commands in the Terminal.
Assuming you're using https://github.com/rest-client/rest-client (since you didn't specify), your require line should be
require 'rest-client'
according to the README. Also, make sure you restart your rails server after adding the gem to your Gemfile and running bundle.
Run the following command in your terminal:
gem install rest-client
and use require 'rest-client'. No need to change to rest_client.
in my case, none of the solutions in this thread worked
what did work, was to add the gem directly in the Gemfile:
gem 'rest-client'
after closing the rails server, exiting rails console and running bundle install,
I opened again the rails console and this time require 'rest-client' worked flawlessly
For me it was an issue with bundle (which I thought I had installed). Spoiler alert, I didn't, and this is how I fixed it. I'm on a Mac running OS X Yosemite and my terminal version is Darwin Kernel Version 14.3.0:
cd
gem install bundler
or
cd
sudo gem install bundler
If you get something along the lines of the following error:
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
Finally, change your require line from:
require 'rest-client'
to
require 'rest_client'
Then run your code!
First ensure you have installed gem 'rest-client', ~> 1.8.0 on your gem file. Run bundle install and then require 'rest_client'. This worked for me.
Try require 'rest_client', instead of require 'rest-client'

rails Can not load a installed Gem

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.

Rails daemon do not start

I have Rails 3.0.3 project and I trying to create daemon by this steps:
http://railscasts.com/episodes/129-custom-daemon
I've installed gem daemons
sudo gem install daemons
Then I've install daemon_generator
rails plugin install https://github.com/dougal/daemon_generator.git
Then created daemon
rails generate daemon game_processor
When I try to run daemon
./lib/daemons/game_processor_ctl start
I got error:
./lib/daemons/game_processor_ctl:2:in `require': no such file to load -- rubygems (LoadError)
from ./lib/daemons/game_processor_ctl:2
Code of daemon:
#!/usr/bin/env ruby
require 'rubygems'
require "daemons"
require 'yaml'
require 'erb'
gem 'activesupport', '>=3.0.0.beta4'
require 'active_support'
# For some reason, ActiveSupport 3.0.0 doesn't load.
# Load needed extension directly for now.
require "active_support/core_ext/object"
require "active_support/core_ext/hash"
options = YAML.load(
ERB.new(
IO.read(
File.dirname(FILE) + "/../../config/daemons.yml"
)).result).with_indifferent_access
options[:dir_mode] = options[:dir_mode].to_sym
Daemons.run File.dirname(FILE) + "/game_processor.rb", options
So, What's wrong? Why it dies, when trying to require rubygems?
Are you on Windows or *nix system - on Windows you should use ruby game_processor.rb start instead of _ctl.
Also as you are using it with Rails - then i think Rails server should be started in required mode as well to make Daemon run properly.

Require a specific version of ActiveRecord

I have both Rails 2.3.4 and Rails 3.0.0.beta installed on my local machine. I am using ActiveRecord in a stand alone ruby script and when I do require 'active_record' 3.0.0.beta is loaded. How can I force it to require 2.3.4 instead? (without uninstalling 3.0.0.beta)
This is covered in the RubyGems manual # http://docs.rubygems.org/read/chapter/4
do:
require 'rubygems'
gem 'activerecord', '= 2.3.4'
A little trick is require 'activerecord' when you want 2.3.5 and 'active_record' when you want 3.0.0.beta.
You have a warning when you using activerecord require but it's load only 2.3.5.
After if you want manage several gem in same computer, you can try rvm and gemset system. It's really great.

How do I get an installed ruby gem included in Rails?

I am attempting to get a gem I installed working in a Rails application. I can require the gem just fine in a Ruby program that I run from the command line using:
require 'nokogiri'
But when I attempt to do the same in one of my Rails controllers it errors saying "no such file to load -- nokogiri".
I tried using the full path to the lib/nokogiri.rb file, but that fails because it cannot find "nokogiri/native".
Better, place the following in your environment.rb file:
Rails::Initializer.run do |config|
...
config.gem :nokogiri
...
end
This will tell Rails that you depend on that particular gem. It also allows you to specify particular versions, and it will automatically keep all your gems synched, or unpack them into vendor/gems if you so wish.
I had a similar error but simply forgot to put the following in my environment.rb file: (note the quoted "nokogiri")
Rails::Initializer.run do |config|
...
config.gem "nokogiri"
...
end
Ok I figured it out. This is going to sound pretty stupid...but oh well...
It turns out I had two installations of ruby on my machine. I use InstantRails to serve my test applications and it comes prepackaged with an installation of ruby. I had another installation however outside of this and it was here that nokogiri had been installed, not in the installation in InstantRails.
In any case they were looking in different spots for the gems.
Try the following
require 'rubygems'
gem 'nokogiri'
If you are on some form of *nix then did you get any errors when you installed the gem, particularly errors stating that the gem was not on the path. This may happen if you have installed the gem as yourself rather than as root and you do not have your personal gem library in your gem path.
If you always install your gems using
sudo gem install some_gem_name
then you should not get that problem.

Resources