1

当我运行 ruby filename.rb时,瘦网络服务器启动,但是当我尝试在 localhost:4567 上查看(Sinatra)应用程序时,我收到此错误消息。谷歌搜索它什么也没发现。有谁知道我可以做些什么来克服这个问题?

gems/eventmachine-0.12.10/lib/em/connection.rb:39:in `block in new': undefined method `associate_callback_target' for #<Thin::Connection:0x00000100fc7500> (NoMethodError)
    from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/eventmachine-0.12.10/lib/em/connection.rb:36:in `instance_eval'
    from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/eventmachine-0.12.10/lib/em/connection.rb:36:in `new'
    from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/eventmachine-0.12.10/lib/eventmachine.rb:1430:in `event_callback'
    from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/eventmachine-0.12.10/lib/pr_eventmachine.rb:815:in `block in eventable_read'
    from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/eventmachine-0.12.10/lib/pr_eventmachine.rb:812:in `times'
    from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/eventmachine-0.12.10/lib/pr_eventmachine.rb:812:in `eventable_read'
    from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/eventmachine-0.12.10/lib/pr_eventmachine.rb:369:in `block in crank_selectables'
    from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/eventmachine-0.12.10/lib/pr_eventmachine.rb:369:in `each'
    from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/eventmachine-0.12.10/lib/pr_eventmachine.rb:369:in `crank_selectables'
    from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/eventmachine-0.12.10/lib/pr_eventmachine.rb:324:in `block in run'
    from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/eventmachine-0.12.10/lib/pr_eventmachine.rb:318:in `loop'
    from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/eventmachine-0.12.10/lib/pr_eventmachine.rb:318:in `run'
    from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/eventmachine-0.12.10/lib/pr_eventmachine.rb:64:in `run_machine'
    from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run'
    from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/thin-1.4.1/lib/thin/backends/base.rb:63:in `start'
    from /Users/michaeljohnmitchell/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/thin-1.4.1/lib/thin/server.rb:159:in `start'
    from /Users/mm/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/rack-1.4.1/lib/rack/handler/thin.rb:13:in `run'
    from /Users/mm/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/sinatra-1.3.3/lib/sinatra/base.rb:1350:in `run!'
    from /Users/mm/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/sinatra-1.3.3/lib/sinatra/main.rb:25:in `block in <module:Sinatra>'

该代码来自一本书 Cloning Internet Applications in Ruby。它的每个演示应用程序都在发生这种情况......

路线代码

get '/' do haml :index end

post '/' do
  uri = URI::parse(params[:original])
  custom = params[:custom].empty? ? nil : params[:custom]
  raise "Invalid URL" unless uri.kind_of? URI::HTTP or uri.kind_of? URI::HTTPS
  @link = Link.shorten(params[:original], custom) 
  haml :index
end

['/info/:short_url', '/info/:short_url/:num_of_days', '/info/:short_url/:num_of_days/:map'].each do |path|
  get path do
    @link = Link.first(:identifier => params[:short_url])
    raise 'This link is not defined yet' unless @link
    @num_of_days = (params[:num_of_days] || 15).to_i
    @count_days_bar = Visit.count_days_bar(params[:short_url], @num_of_days)
    chart = Visit.count_country_chart(params[:short_url], params[:map] || 'world')
    @count_country_map = chart[:map]
    @count_country_bar = chart[:bar]
    haml :info
  end
end

get '/:short_url' do 
  link = Link.first(:identifier => params[:short_url])
  link.visits << Visit.create(:ip => get_remote_ip(env))
  link.save
  redirect link.url.original, 301
end

error do haml :index end

def get_remote_ip(env)
  if addr = env['HTTP_X_FORWARDED_FOR']
    addr.split(',').first.strip
  else
    env['REMOTE_ADDR']
  end
end
4

2 回答 2

1

我是Cloning Internet Applications with Ruby一书的作者。我尝试在 GitHub 存储库中运行代码,并且除非进行一些更改(由于 Sinatra 中的更改),否则我可以成功运行它。也许您可以尝试 Prakash Murthy 给出的答案,以升级到 EventMachine 1.0.0,但我正在运行与您相同的版本(0.12.10),这不会给我带来任何问题。

如果您直接给我发电子邮件,我们可以解决这个问题(稍后更新 Stack Overflow)。

于 2012-10-05T06:15:31.357 回答
0

我在使用 Ruby 1.9.2 时遇到了这个问题。Eventmachine 的两个版本(0.12.10 和 1.0.x)都提出了这个问题。根据链接的 Github 问题中的提示,我切换到 Ruby 2.1.1 并重新捆绑;安装了 Eventmachine 1.0.3 并且问题消失了。

(这整件事对我来说涉及很多剃须,包括将我的 Mac 更新到 Mavericks,吹走许多过时的库,以及重新安装 Xcode 和 Homebrew - 希望对其他人来说更简单。)

于 2014-06-16T16:04:39.667 回答