2012-09-04 44 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目錄在庫路徑上。這使得依賴更加明確,但是我不確定它具有更多的交流性。

相關問題