我如何需要ActiveSupport的rescue_from方法?

时间:2014-09-25 21:32:29

标签: ruby-on-rails module activesupport

我在application controller中有这段代码:

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

我想把它移到一个模块然后:

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

现在我的模块看起来像:

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

module ExceptionMailer

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

我得到了:undefined method 'rescue_from' for ExceptionMailer:Module

我已经用谷歌搜索了如何在模块中包含rescue_from?&#39; - 我还有点失落。

1 个答案:

答案 0 :(得分:14)

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

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

end