gpt4 book ai didi

ruby-on-rails - 使用带有 Resque 后台作业的 Controller 方法

转载 作者:可可西里 更新时间:2023-11-01 11:20:35 24 4
gpt4 key购买 nike

我在 Rails 4 上使用带 Redis 的 Resque。

我的问题:如何在我的后台作业中使用当前在我的 application_controller 中定义的 Controller 方法?

这是我定义的当前方法:

def push_to_google(token, message)
if token.present?
gcm = GCM.new("843jf9384fj839f848j890fj3")
registration_ids = ["#{token}"] # an array of one or more client registration tokens
options = {data: {notification: "#{message}"}}
response = gcm.send(registration_ids, options)
end
end

我想在我的 delayed_notifications 中定义的这个后台作业中使用它:

class DelayedNotifications
@queue = :notifications_queue

def self.perform(registration_id, user_name)
push_to_google(registration_id, "New message from #{user_name}.")
end
end

当然,我的工作目前因以下错误而失败:

DelayedNotifications:Class 的未定义方法“push_to_google”

提前感谢您的帮助。

最佳答案

提取(移动)push_to_googleApplicationHelper 并在您的 ApplicationController 中包含 ApplicationHelper延迟通知

更改后,您的application_helper.rb应该是:

module ApplicationHelper
# other methods

def push_to_google(token, message)
if token.present?
gcm = GCM.new("843jf9384fj839f848j890fj3")
registration_ids = ["#{token}"] # an array of one or more client registration tokens
options = {data: {notification: "#{message}"}}
response = gcm.send(registration_ids, options)
end
end
end

application_controller.rb:

class ApplicationController < ActionController::Base
include ApplicationHelper

end

delayed_notifications.rb:

class DelayedNotifications
include ApplicationHelper

@queue = :notifications_queue

def self.perform(registration_id, user_name)
push_to_google(registration_id, "New message from #{user_name}.")
end
end

关于ruby-on-rails - 使用带有 Resque 后台作业的 Controller 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36244258/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com