Trouble including httparty in ruby on rails

浪子不回头ぞ 提交于 2019-12-05 10:21:28

问题


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?


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




回答2:


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




回答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.




回答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




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



来源:https://stackoverflow.com/questions/4625577/trouble-including-httparty-in-ruby-on-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!