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'
Related
I need to make changed to the Twitter gem, so I cloned it to a local folder, then changed my bundle file like so:
gem 'twitter', :path => '~/code/twitter-ruby'
I added a new method, and ran a test that simply calls the method, to start off with. But I get an error of undefined method. In other words, it hasn't detected my changes. I tried commenting out a whole file, ran my other tests and those all worked, as though the file were still there.
In my project I'm still importing as require 'twitter'.
I've tried running bundle update twitter, I've tried removing it from the bundle, I've tried increasing the version number (which it does detect, and updates to the most recent version), I've tried committing my changes to GIT. Nothing seems to work - I just keep getting the same undefined method error. Any ideas?
EDIT: my Gemfile.lock:
PATH
remote: /Users/Andrew/code/twitter
specs:
twitter (5.15.0)
GEM
remote: https://rubygems.org/
specs:
PLATFORMS
ruby
DEPENDENCIES
twitter!
BUNDLED WITH
1.12.4
My issue was that I was using bundle to install the gems, but I wasn't using bundle exec to run my application. It became clear after running the uninstall suggestion and the require my_lib line starting to fail inside irb. bundle exec irb allowed it to work using the gem path defined in the Gemfile.
try this out:
First uninstall the twitter gem, by doing gem uninstall twitter, then do bundle install
Try giving the branch name too.
gem 'twitter', path: <absolute-path-to-your-local-twitter-repo>, branch: <branch-name>
EDIT
e.g. path as "/Users/me/path/to/repo"
And run
bundle update twitter
Then restart server, if running as a web application.
I am developing a gem meant to be used with Rails projects and want to try it out locally with another Rails app of mine.
I built the gem with bundle exec rake release which put a .gem file in the /pkg directory.
Then, in my Rails app, I added the following to my gemfile
gem 'mygem', '0.1.1', path: '/Users/me/projects/mygem/ruby/pkg'
I then ran bundle install which said it installed the gem. When I do this, it removes the gem from the path. IDK where it went.
When I start the Rails app, it's like the gem isn't included at all.
Interestingly, if I add a version that doesn't even exist, it still says bundle install works fine. (Example: gem 'mygem', '0.1.2345', path: '/Users/me/projects/mygem/ruby/pkg')
What am I supposed to do to try out my Gem locally with a Rails app?
This question is different from How can I specify a local gem in my Gemfile? because I explicitly tell bundle in my Gemfile to use the local gem, with the path given, and it still doesn't work. When I run bundle install, it says
Using mygem 0.1.1 from source at /Users/me/projects/mygem/pkg
So you'd think it works right, but it still doesn't.
Interestly, if I try it with a version number that doesn't exist, like mygem 1.2.3, it still runs bundle install successfully, which is really weird and seems like a bug:
Using mygem 1.2.3 (was 0.1.1) from source at /Users/me/projects/mygem/pkg
I prefer to use the following when working on a local gem side-by-side with a Rails project:
gem 'foo',
:git => '/path/to/local/git/repo',
:branch => 'my-fancy-feature-branch'
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.
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
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.