Rails:我如何需要 ActiveSupport 的rescue_from 方法? [英] Rails: How do I require ActiveSupport's rescue_from method?

查看:50
本文介绍了Rails:我如何需要 ActiveSupport 的rescue_from 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 application controller 中有这个代码:

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|
...

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

我在谷歌上搜索过如何在模块中包含rescue_from?"——我还是有点迷茫.

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

推荐答案

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

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

end

这篇关于Rails:我如何需要 ActiveSupport 的rescue_from 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆