0

この問題の解決策が何日も見つかりませんでした。私は Ruby on Rails の初心者なので、明らかな何かが欠けていると確信しています。

新しいイベントごとに作成したいツイートにリンクするために、Web サイトの URL を短縮しようとしています。サイトで新しいイベントを作成すると、次のエラーが表示されます。

NameError: 未定義のローカル変数またはメソッドbitly' for main:Object from (irb):7 from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.12/lib/rails/commands/console.rb:47:instart' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.12/lib/rails/commands/console.rb:8:in start' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.12/lib/rails/commands.rb:41:in' from script/rails:6:in require' from script/rails:6:in'

bitly gemを使用しています。

私の宝石ファイルには、次のものがあります。

gem 'bitly', "~> 0.9.0"

config/initializers に bitly.rb ファイルを作成しました。

Bitly.configure do |config|
  config.api_version = 3
  config.login = "xxxx"
  config.api_key = "yyyy"
end

私のイベントモデルには、次のものがあります。

after_create :tweet_event

def tweet_event
    client = Bitly.client
    u = bitly.shorten('http://www.weinevents.de')
    u = u.short_url
    tweet = title + " " + u
    send_tweet(tweet)
end

私の application_helper.rb ファイルには、次のものがあります。

def send_tweet(tweet)
    client = Twitter::Client.new
    client.update(tweet)   
end

そして、私のapplication_controllerには

require 'bitly'
4

1 に答える 1

1

これはタイプミスです。あなたが持っていbitly.shorten('http://www.weinevents.de')ます。ドキュメントに と記載されている場合、 はbitly.shortenbitlyという名前のクライアント オブジェクトを指しますclient。したがって:

client.shorten('http://www.weinevents.de')
于 2013-09-30T21:24:16.333 に答える