19

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.

Nunser
  • 4,512
  • 8
  • 25
  • 37
Jerrod
  • 195
  • 1
  • 1
  • 5

6 Answers6

20

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.

deefour
  • 34,974
  • 7
  • 97
  • 90
7

Run the following command in your terminal:

gem install rest-client

and use require 'rest-client'. No need to change to rest_client.

Genzotto
  • 1,954
  • 6
  • 26
  • 45
akvenk
  • 466
  • 3
  • 8
5

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

Salomanuel
  • 897
  • 10
  • 22
1

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!

schro's cat
  • 119
  • 4
0

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.

Rahul Mane
  • 1,005
  • 18
  • 33
-1

Try require 'rest_client', instead of require 'rest-client'

revolver
  • 2,385
  • 5
  • 24
  • 40