How do I require ActiveSupport's rescue_from method?

橙三吉。 提交于 2020-01-12 14:10:51

问题


I have this code in application controller:

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

I want to move this into a module and then:

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

Right now my module looks like:

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

module ExceptionMailer

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

And I am getting: undefined method 'rescue_from' for ExceptionMailer:Module

I've googled 'how do i include rescue_from in a module?' -- and I'm still a little lost.


回答1:


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

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

end


来源:https://stackoverflow.com/questions/26048483/how-do-i-require-activesupports-rescue-from-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!