如何向现有的通知负载添加属性? [英] How to add attribute to existing Notifications payload?

查看:47
本文介绍了如何向现有的通知负载添加属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Rails 通知中,我订阅了process_action.action_controller",并希望向有效负载添加更多属性.我该怎么做?

In Rails notifications, I am subscribing to "process_action.action_controller", and would like to add more attributes to the payload. How can I do that?

我曾尝试使用 append_info_to_payload,但这似乎不起作用.

I have tried using append_info_to_payload, but this seems to do nothing.

module AppendExceptionPayload
  module ControllerRuntime
    extend ActiveSupport::Concern

    protected 

    def append_info_to_payload(payload)
      super
      payload[:happy] = "HAPPY"
    end
  end
end

订阅和上面的代码在 Rails 引擎中,所以这是我调用添加它的地方:

The subscription and above code is in a Rails engine, so this is where I make the call to add it:

require 'append_exception_payload'

module Instrument
  class Engine < ::Rails::Engine

    ActiveSupport.on_load :action_controller do
      include AppendExceptionPayload::ControllerRuntime
    end

  end
end

推荐答案

悬赏之后,我自己找到了解决方案.Rails 处理得非常干净.

After putting up the bounty, I found a solution myself. Rails handles this really cleanly.

基本上,append_info_to_payload 方法正是为此而设计的.

Basically, the append_info_to_payload method is meant exactly for this.

为了包含会话信息和登录用户信息,我将其添加到我的 application_controller.rb 中:

So to include session information and signed_in user information I added this to my application_controller.rb:

def append_info_to_payload(payload)
    super
    payload[:session] = request.session_options[:id] rescue ""
    payload[:user_id] = session[:user_id] rescue "unknown"
end

这篇关于如何向现有的通知负载添加属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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