【问题标题】:Rails + bunny gem - no such file to load -- bunnyRails + bunny gem - 没有要加载的文件 - bunny
【发布时间】:2013-07-04 11:47:59
【问题描述】:

我有一个 Rails 控制器:

class Console::AccesorBillsController < ConsoleController
  ...
  def bill
    AcceccorBill.new.run

    render :nothing => true
  end
end

我在那里创建新模型并使用兔子页面 (http://rubybunny.info/articles/getting_started.html) 中的示例运行方法:

require "bunny"

class AcceccorBill
  def initialize()
  end

  def run
    conn = Bunny.new
    conn.start

    ch  = conn.create_channel
    x   = ch.fanout("nba.scores")

    ch.queue("joe",   :auto_delete => true).bind(x).subscribe do |delivery_info, metadata, payload|
      puts "#{payload} => joe"
    end

    ch.queue("aaron", :auto_delete => true).bind(x).subscribe do |delivery_info, metadata, payload|
      puts "#{payload} => aaron"
    end

    ch.queue("bob",   :auto_delete => true).bind(x).subscribe do |delivery_info, metadata, payload|
      puts "#{payload} => bob"
    end

    x.publish("BOS 101, NYK 89").publish("ORL 85, ALT 88")

    conn.close
  end
end

但我总是得到这个错误:

NameError - uninitialized constant AcceccorBill::Bunny:
  (gem) activesupport-3.1.12/lib/active_support/dependencies.rb:507:in `load_missing_constant'
  (gem) activesupport-3.1.12/lib/active_support/dependencies.rb:181:in `block in const_missing'
  (gem) activesupport-3.1.12/lib/active_support/dependencies.rb:179:in `const_missing'
  (gem) rake-0.9.2.2/lib/rake/ext/module.rb:36:in `const_missing'
  app/ext/acceccor_bill.rb:6:in `initialize'
  app/controllers/console/accesor_bills_controller.rb:17:in `bill'
  (gem) actionpack-3.1.12/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  (gem) actionpack-3.1.12/lib/abstract_controller/base.rb:167:in `process_action'
  (gem) actionpack-3.1.12/lib/action_controller/metal/rendering.rb:10:in `process_action'

有人知道怎么解决吗?

P.S.:我通过 bundler 安装了 gem,看起来 require 不起作用。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 bundler rabbitmq


    【解决方案1】:

    解决了。

    我忘了要求bundler。所以我只是在文件的开头添加了这个:

    require "bundler/setup"
    require "bunny"
    

    仅此而已。

    【讨论】:

    • 每次调用Console::AccesorBillsController.bill,都会创建一个新的Bunny 连接。我认为如果可以将连接重用于发布/订阅,它应该只创建一次。您是否更好地重构了代码?如果是这样,我想知道你是怎么做到的。谢谢!
    • @philipjkim 这只是一个测试兔子工作的例子,仅此而已。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    相关资源
    最近更新 更多