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 に答える