在 ruby​​ on rails 中包含 httparty 的问题 [英] Trouble including httparty in ruby on rails

查看:52
本文介绍了在 ruby​​ on rails 中包含 httparty 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在 Rails 代码中使用 HTTParty

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"

当我在 Rails 应用程序中尝试此操作时

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

我收到错误消息没有要加载的此类文件 -- httparty"

I get the error message "no such file to load -- httparty"

我怀疑我的环境有问题?

I suspect there is something wrong with my environment?

推荐答案

您不需要在 Controller 中执行 'include HTTParty'.只需删除它,它应该可以工作.我刚刚测试了它,它对我有用.如果这对您不起作用,您应该将 gem 添加到您的环境中.

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.

通常,如果您在 Rails 应用程序中使用 gem,您应该在 environment.rb 中添加以下内容:

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

config.gem "httparty"

gem 现在将在应用程序中可用,您无需在控制器中添加require".此外,您不需要在控制器中使用 RubyGems.

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.

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

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

gem "httparty"

希望对你有用.:)

这篇关于在 ruby​​ on rails 中包含 httparty 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆