2009-10-01 126 views

回答

29

我想出瞭如何做到這一點。以下是您將放入控制器以觸發電子郵件的代碼。

對於Rails 2.3 version of the Exception_Notification plugin

begin 
    10/0 
rescue Exception => e 
    ExceptionNotifier.deliver_exception_notification(e, self, request) 
end 

對於Rails 3 version of the Exception_Notification plugin

begin 
    10/0 
rescue Exception => e 
    ExceptionNotifier::Notifier.exception_notification(request.env, e).deliver 
end 

對於Rails 4 version (currently v4.0.1 of the exception_notification gem):

begin 
    some code... 
rescue => e 
    ExceptionNotifier.notify_exception(e) 
    ExceptionNotifier.notify_exception(e, env: request.env, data: { message: "oops" }) 
end 
+3

如何從模型做到這一點,因爲我想裏面添加搶救塊模型的方法。如何從模型內部獲取request.env? – 2011-10-12 09:46:36

+0

@DonnyKurnia您可以放入'{}'而不是'request.env',儘管在電子郵件中有一個關於無法訪問會話和請求數據的錯誤。 – 2013-02-19 00:46:16

+0

有沒有辦法訪問請求。我試圖在由控制器調用的非模型類中訪問它。 – Rpant 2017-06-21 03:11:00

0

例外通知程序專門用於捕獲未被捕獲的錯誤。一旦你發現錯誤,你自己發送電子郵件。快速而骯髒的方法是在捕獲異常時觸發異常郵件程序代碼。我無法回想起我的頭頂上的方法,但快速查看插件應該會產生結果。查找郵件代碼的render_exception_in_public(或類似的東西)。