2014-09-25 15 views
7

Tôi có mã này trong application controller:Làm cách nào để yêu cầu phương thức rescue_from của ActiveSupport?

# Method to capture and handle all exceptions 
rescue_from Exception do |ex| 
    Rails.logger.debug ex 
    do_stuff(ex) 
end 

tôi muốn chuyển sang dạng mô-đun và sau đó:

class ApplicationController < ActionController::Base 
    include 'module' 
... 

Ngay bây giờ mô-đun của tôi trông giống như:

# lib/exception_mailer.rb 
require 'action_mailer' 
require 'active_support' 

module ExceptionMailer 

    # Method to capture and handle all exceptions 
    rescue_from Exception do |ex| 
... 

Và tôi đang nhận được: undefined method 'rescue_from' for ExceptionMailer:Module

Tôi đã googled 'làm thế nào để tôi bao gồm rescue_from trong một mô-đun?' - và tôi vẫn còn một chút mất mát.

+0

Liên kết này có thể giúp bạn. http://apidock.com/rails/ActiveSupport/Rescuable/ClassMethods/rescue_from – Joel

+0

Tôi nghĩ rằng tôi đã tìm thấy một giải pháp làm 'mở rộng ActiveSupport :: Quan tâm' và sử dụng một khối' bao gồm do'. Rails là một phụ thuộc của đá quý của tôi. Tôi hiện không cần yêu cầu gì cả. –

Trả lời

12
module Exceptionailer 
    # http://api.rubyonrails.org/classes/ActiveSupport/Concern.html 
    extend ActiveSupport::Concern 

    included do 
    rescue_from Exception do |ex| 
     ... 
    end 
    end 

end 
Các vấn đề liên quan