2012-09-04 45 views
1

我创建Ruby类,并把它放在APP_DIR/lib中/ appointment_messaging_job.rb这是什么未初始化的常量错误?

class AppointmentMessagingJob 
    def perform 
    end 
end 

在我的控制器的一个我这样做:

test = AppointmentMessagingJob.new 

我得到一个非常讨厌的很难以理解的错误:

Started GET "/en/appointments/1/approve" for 127.0.0.1 at 2012-09-04 13:02:43 -0400 
Processing by AppointmentsController#approve as HTML 
    Parameters: {"locale"=>"en", "id"=>"1"} 
Completed 500 Internal Server Error in 2ms 

NameError (uninitialized constant AppointmentsController::AppointmentMessagingJob): 
    app/controllers/appointments_controller.rb:89:in 'approve' 

什么是未初始化的常量?我真的不明白。

回答

4

你的库没有被加载,它无法解析类名。

更新您的config/application.rb以自动包含lib目录;书中有一条线,被注释掉了:

# Custom directories with classes and modules you want to be autoloadable. 
# config.autoload_paths += %W(#{config.root}/extras) 

(你会希望包括lib目录,而不是extras,很明显。)

IIRC你也可以在使用require 'test_class'你的控制器,lib目录在库路径上。这使得依赖更加明确,但是我不确定它具有更多的交流性。

相关问题