在ruby on rails中包含httparty的问题

时间:2022-04-27 04:04:03

I've been trying to use HTTParty in my rails code

我一直试图在rails代码中使用http。

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

当我在rails应用中尝试这个时

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"

我得到了错误信息"没有要加载的文件- httparty"

I suspect there is something wrong with my environment?

我怀疑我的环境有问题?

5 个解决方案

#1


21  

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.

不需要在控制器中写入“include HTTParty”。只要移除它,它就会工作。我刚刚测试过,它对我有用。如果这对您不起作用,您应该将gem添加到您的环境中。

Usually if you use a gem inside your Rails application, you should add the following to environment.rb:

通常,如果您在Rails应用程序中使用gem,应该将以下内容添加到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.

gem现在可以在应用程序中使用了,您不需要在控制器中添加“require”。此外,您不需要在控制器中要求RubyGems。

When you use Rails 3, you need to put the following inside the Gemfile:

使用Rails 3时,需要在Gemfile中加入以下内容:

gem "httparty"

I hope it works for you. :)

我希望它对你有用。:)

#2


7  

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

问题是,如果您加载了一个新的gem,即使您正在开发,也必须重新启动服务器。

#3


4  

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.

我犯了同样的错误。我试着把require HTTParty移动了一遍,但是我发现,我需要做的就是重新启动rails服务器,最后我不需要“要求HTTParty”或“包含”它。它只是需要加载到rails中。

#4


1  

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.

遇到了同样的问题。然后我从Ruby 1.8.7切换到Ruby 1.9.2,所有的错误都烟消云散了。

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

(是的,我花了好几个小时才想到Ruby版本可能是问题所在。配置了一个secundairy服务器,以避免可能与两个ruby版本的冲突,并且在经过了许多小时之后,我得到了我的RoR堆栈并运行。使用httparty进行的第一个测试(基于上面的示例)是开箱即用的!终于可以安心睡觉了:-)

#5


0  

1)include the httpary in your gemfile

1)在gemfile中包含httpary

open your gem file then add

打开您的gem文件然后添加。

 gem 'httparty','YOUR VERSION NUMBER'

2) run bundle install in your command prompt of the app file

2)在app文件的命令提示符中运行bundle install

3) restart the server

3)重新启动服务器

#1


21  

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.

不需要在控制器中写入“include HTTParty”。只要移除它,它就会工作。我刚刚测试过,它对我有用。如果这对您不起作用,您应该将gem添加到您的环境中。

Usually if you use a gem inside your Rails application, you should add the following to environment.rb:

通常,如果您在Rails应用程序中使用gem,应该将以下内容添加到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.

gem现在可以在应用程序中使用了,您不需要在控制器中添加“require”。此外,您不需要在控制器中要求RubyGems。

When you use Rails 3, you need to put the following inside the Gemfile:

使用Rails 3时,需要在Gemfile中加入以下内容:

gem "httparty"

I hope it works for you. :)

我希望它对你有用。:)

#2


7  

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

问题是,如果您加载了一个新的gem,即使您正在开发,也必须重新启动服务器。

#3


4  

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.

我犯了同样的错误。我试着把require HTTParty移动了一遍,但是我发现,我需要做的就是重新启动rails服务器,最后我不需要“要求HTTParty”或“包含”它。它只是需要加载到rails中。

#4


1  

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.

遇到了同样的问题。然后我从Ruby 1.8.7切换到Ruby 1.9.2,所有的错误都烟消云散了。

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

(是的,我花了好几个小时才想到Ruby版本可能是问题所在。配置了一个secundairy服务器,以避免可能与两个ruby版本的冲突,并且在经过了许多小时之后,我得到了我的RoR堆栈并运行。使用httparty进行的第一个测试(基于上面的示例)是开箱即用的!终于可以安心睡觉了:-)

#5


0  

1)include the httpary in your gemfile

1)在gemfile中包含httpary

open your gem file then add

打开您的gem文件然后添加。

 gem 'httparty','YOUR VERSION NUMBER'

2) run bundle install in your command prompt of the app file

2)在app文件的命令提示符中运行bundle install

3) restart the server

3)重新启动服务器