Ruby on rails Wisper:未收听广播的用户

Ruby on rails Wisper:未收听广播的用户,ruby-on-rails,publish-subscribe,wisper,Ruby On Rails,Publish Subscribe,Wisper,在前面的一个问题中,有人建议我使用名为Wisper的宝石。我很高兴了解它,因为它正是我正在寻找的解决方案。从Wisper上的文档中我无法理解的是侦听器是如何注册自己的 这是我的密码: app/models/subscription.rb class Subscription < ActiveRecord::Base include Wisper::Publisher def some_method # some code here broadcast(:subscri

在前面的一个问题中,有人建议我使用名为Wisper的宝石。我很高兴了解它,因为它正是我正在寻找的解决方案。从Wisper上的文档中我无法理解的是侦听器是如何注册自己的

这是我的密码:

app/models/subscription.rb

class Subscription < ActiveRecord::Base
include Wisper::Publisher

  def some_method
    # some code here
    broadcast(:subscription_paused)
  end
end
我也尝试过,类似于Wiki中的示例:

subscription = Subscription.new
subscription.subscribe(Offer.new)

我错过了什么?(我真的不确定上面的代码是否应该包含在初始值设定项中。)

如果提供和订阅模型中存在表,那么代码应该可以工作

在rails控制台中尝试以下操作:

# class Subscription < ActiveRecord::Base
class Subscription
  include Wisper::Publisher

  def some_method
    # some code here
    broadcast(:subscription_paused)
  end
end

# class Offer < ActiveRecord::Base
class Offer
  def subscription_paused
    puts "jeijjj"
  end
end


Wisper.subscribe(Offer.new)

Subscription.new.some_method

您的代码是正确的,您能否确认
Offer.new.subscription\u paused
将启动pry。我还建议您提出一个异常,而不是
binding.pry
,以防万一。您还可以使用
Wisper::GlobalListeners获得所有全局订阅侦听器的列表。侦听器
,是该列表中的
Offer
对象吗?谢谢。我的问题很愚蠢。我无意中在Offer的protected部分定义了侦听器方法。现在一切都很好!:)
subscription = Subscription.new
subscription.subscribe(Offer.new)
# class Subscription < ActiveRecord::Base
class Subscription
  include Wisper::Publisher

  def some_method
    # some code here
    broadcast(:subscription_paused)
  end
end

# class Offer < ActiveRecord::Base
class Offer
  def subscription_paused
    puts "jeijjj"
  end
end


Wisper.subscribe(Offer.new)

Subscription.new.some_method
"jeijjj"