外部模块中的 Rails Resque 未定义方法错误 [英] Rails Resque undefined method error in external module
问题描述
我无法从 resque Worker 中包含的模块调用方法.在下面的示例中,当我尝试调用 worker 内部的 say
方法(在 TestLib 模块中)时,我不断收到未定义的方法错误.为了说明问题,我已将代码简化为最基本的代码:
I'm having trouble calling methods from an included module inside a resque worker. In the example below, I keep getting undefined method errrors when I attempt to call the say
method inside the worker (which is in the TestLib module). I've reduced the code down to bare basics to illustrate the issue:
控制器(/app/controllers/test_controller.rb)
Controller (/app/controllers/test_controller.rb)
class TestController < ApplicationController
def testque
Resque.enqueue( TestWorker, "HI" )
end
end
图书馆(/lib/test_lib.rb)
Library (/lib/test_lib.rb)
module TestLib
def say( word )
puts word
end
end
工人(/workers/test_worker.rb)
Worker (/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)
Rakefile (resque.rake)
require "resque/tasks"
task "resque:setup" => :environment
我正在使用以下命令运行 resque:rake environment resque:work QUEUE='*'
I'm running resque using the following command: rake environment resque:work QUEUE='*'
宝石:导轨 (3.0.4)Redis (2.2.2)redis 命名空间 (1.0.3)resque (1.19.0)
Gems: rails (3.0.4) redis (2.2.2) redis-namespace (1.0.3) resque (1.19.0)
服务器:nginx/1.0.6
Server: nginx/1.0.6
有人对那里发生的事情有任何想法吗?
Anyone have any ideas as to what's going on there?
推荐答案
当你包含一个模块时,它的方法成为实例方法.当你扩展时,它们变成了类方法.您只需要将 include TestLib
更改为 extend TestLib
就可以了.
When you include a module, its methods become instance methods. When you extend, they become class methods. You just need to change include TestLib
to extend TestLib
and it should work.
这篇关于外部模块中的 Rails Resque 未定义方法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!