0

我正在使用基于此示例的足够简单的实现:http: //www.appelsiini.net/2010/using-bitly-with-httparty

我把这个文件放在lib文件夹(bitly.rb)

require 'rubygems'
require 'httparty'
class Bitly
  include HTTParty
  base_uri "api.bit.ly"

  @@login   = "goldhat"
  @@api_key = "R_xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

  def self.shorten(url)
    response = get("/v3/shorten?login=#{@@login}&apiKey=#{@@api_key}&longUrl=#{url}")
    response["data"]["url"]
  end
end

出于某种原因,我得到了一个 nil 对象。好像 bitly 甚至没有响应任何数据。我在我的控制台和我的应用程序中对其进行了测试,并得到了同样的错误:

NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
        from c:/goldhat_production/lib/bitly.rb:9:in `shorten'
        from (irb):1

有任何想法吗?

4

2 回答 2

1

DIY 的替代方案 - 您可以在此处为 bit.ly 找到经过充分测试的 Ruby gem 。

于 2011-03-13T06:18:06.330 回答
0

问题不在于 Bitly 代码,而在于您将它放在 Rails 3 应用程序的 lib/ 目录中。要加载 lib/,您需要以下内容config/application.rb

config.autoload_paths += %W(#{config.root}/lib)
于 2011-03-13T07:02:29.853 回答