加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails 4 ActionMailer around_action |访问操

发布时间:2020-12-17 03:18:42 所属栏目:百科 来源:网络整理
导读:我正在为我的customer_mailer类构建一个around_action,这样我每次调用deliver_now时都不需要包装开始和救援 class CustomerMailer ApplicationMailer around_action :rescue_error def send_email(customer) ... end def invite_friend(customer,invitee_ema
我正在为我的customer_mailer类构建一个around_action,这样我每次调用deliver_now时都不需要包装开始和救援

class CustomerMailer < ApplicationMailer
  around_action :rescue_error

  def send_email(customer)
    ...
  end

  def invite_friend(customer,invitee_email)
    ...
  end

  private
    def rescue_error
      yield
    rescue => e
      msg = "Caught exception! #{e} | #{action_name}"
      puts msg
      raise
     end
end

所以在救援中,我想记录消息,例如调用了哪个动作,我设法找到方法action_name来显示调用了哪个动作,但我找不到一种方法来检索传入的参数行动,任何想法?

谢谢!

解决方法

在我回答你的问题之前:在你的情况下会使用Bugsnag或类似的工作吗?另外,rescue_from Exception,使用:: exception_handler为你工作? (虽然不允许你再加上例外)

我挖掘Rails源代码,似乎参数不存储在任何地方.它们只是作为splat传递给您的邮件程序类中定义的实例方法.但是,有一种方法可以存储它们(没有猴子修补).

邮件程序继承自AbstractController :: Base.看下面的代码:

# Call the action. Override this in a subclass to modify the
  # behavior around processing an action. This,and not #process,# is the intended way to override action dispatching.
  #
  # Notice that the first argument is the method to be dispatched
  # which is *not* necessarily the same as the action name.
  def process_action(method_name,*args)
    send_action(method_name,*args)
  end

  # Actually call the method associated with the action. Override
  # this method if you wish to change how action methods are called,# not to add additional behavior around it. For example,you would
  # override #send_action if you want to inject arguments into the
  # method.
  alias send_action send

我们可以看到我们可以覆盖#send_action并使其存储参数.将以下内容添加到ApplicationMailer:

class ApplicationMailer < ActionMailer::Base
  def send_action(method_name,*args)
    @action_args = args
    super
  end
end

参数将在所有邮件中以@action_args的形式提供.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读