Trouble including httparty in ruby on rails

20,212

Solution 1

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. :)

Solution 2

The problem is, if you load a new gem, you have to restart the server even if you are in development.

Solution 3

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.

Solution 4

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

Solution 5

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 :-)

Share:
20,212
AllDayer
Author by

AllDayer

C#, Xamarin

Updated on July 09, 2022

Comments

  • AllDayer
    AllDayer almost 2 years

    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?

  • fulvio
    fulvio over 13 years
    I can confirm it's working on my end without including httparty.
  • owl
    owl over 13 years
    I would guess that he's using Rails 3 and that he hasn't put `gem 'httparty' in his Gemfile.
  • AllDayer
    AllDayer over 13 years
    Thanks for the reply. I had httparty in the gem file, but I've now removed the two requires and also the include. I now get the error "uninitialized constant FooController::HTTParty". I must be missing something really obvious, time for sleep I think.
  • RobinBrouwer
    RobinBrouwer over 13 years
    Have you done a 'bundle update' inside your terminal after adding httparty to the Gemfile? I just tested it by putting the gem inside the Gemfile and it works great.
  • AllDayer
    AllDayer over 13 years
    I ended up loading this in a different project where it worked. Came back to the original project where it wasn't working, and presto, working. :-)
  • Rickmasta
    Rickmasta over 13 years
    When I try adding 'config.gem "httparty"' to my environment.rb, I get this error when trying to start the rails server, "C:/Users/Ricky/dbapitest/config/environment.rb:1: undefined local variable or me thod `config' for main:Object (NameError)"
  • RobinBrouwer
    RobinBrouwer over 13 years
    @Rickmasta: You need to make sure you put it inside the block. So between 'Rails::Initializer.run do |config|' and 'end'.
  • Excalibur
    Excalibur over 11 years
    @Allayer: You might have done the same thing I did. Add to Gemfile, run bundle install, then forget to restart rails server! Maybe... Anyway, doing that did the trick for me.
  • dee
    dee about 11 years
    I'm having this same error when trying to use HTTParty from a service object in Rails 4. Including it causes the error, not including it then calling it HTTPart.get() causes an unintialized constant error Any ideas?