【问题标题】:Rails Resque undefined method error in external module外部模块中的 Rails Resque 未定义方法错误
【发布时间】:2011-12-29 03:02:50
【问题描述】:

我在调用 resque worker 中包含的模块的方法时遇到问题。在下面的示例中,当我尝试在工作程序(位于 TestLib 模块中)中调用 say 方法时,我不断收到未定义的方法错误。为了说明问题,我已将代码简化为最基本的:

控制器 (/app/controllers/test_controller.rb)

class TestController < ApplicationController
  def testque
    Resque.enqueue( TestWorker, "HI" )
  end
end

图书馆 (/lib/test_lib.rb)

module TestLib
  def say( word )
    puts word
  end
end

工人 (/workers/test_worker.rb)

require 'test_lib'

class TestWorker
  include TestLib

  @queue = :test_queue

  def self.perform( word )
    say( word ) #returns: undefined method 'say' for TestWorker:Class
    TestLib::say( word ) #returns: undefined method 'say' for TestLib::Module
  end
end

Rakefile (resque.rake)

require "resque/tasks"
task "resque:setup" => :environment

我正在使用以下命令运行 resque:rake environment resque:work QUEUE='*'

宝石: 导轨 (3.0.4) redis (2.2.2) redis 命名空间 (1.0.3) resque (1.19.0)

服务器: nginx/1.0.6

有人对那里发生的事情有任何想法吗?

【问题讨论】:

    标签: ruby-on-rails resque worker


    【解决方案1】:

    当你包含一个模块时,它的方法变成了实例方法。当您扩展时,它们成为类方法。您只需将include TestLib 更改为extend TestLib 即可。

    【讨论】:

    • headslap 我想我最近在 Rails 上工作太多了。那成功了。谢谢!
    • 如果你这样做了还是不行,重启你的服务器
    猜你喜欢
    • 2012-05-21
    • 2015-01-22
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多