Rails服务从未调用过

时间:2017-07-12 08:59:21

标签: ruby-on-rails

我已经编写了一个简单的服务来创建我想要通过ActionCable连接发送的数据,但从开发日志判断它看起来好像从未执行过,我无法弄清楚原因。< / p>

#app\channels\quiz_data_channel.rb:
class QuizDataChannel < ApplicationCable::Channel
  def subscribed
    stream_from specific_channel
  end

  def send_data
    logger.debug "[AC] send data method entered" #<-- this part is executed

    QuizDataCreation.new(user: current_user).create  #<-- calling the service HERE

    logger.debug "[AC] service created new quiz data"  #<-- not executed

    #after I fix this issue I would broadcast the data here
  end

  private
  def specific_channel
    "quiz_data_#{params[:access_key]}"
  end
end

#app\services\quiz_data_creation.rb:
module Services
  class QuizDataCreation

    def initialize(user)
      self.user = user
      logger.debug "[AC] service - initialize user: #{self.user.inspect}"  #<-- not executed
    end

    def create
      logger.debug "[AC] Service entered!"  #<-- not executed
    end
  end
end

调用send_data方法有效,并且第一个文本(&#34; [AC]发送数据方法输入&#34; )将被打印到开发日志中,但那就是它。我一直在查看几个教程,并尝试将服务放在模型的子文件夹中,然后调用Services::QuizDataCreation.new(user: current_user).create,但还没有让它工作。我确信有一个非常明显的解决方案,但我无法看到它,所以我会非常感谢任何指针。

1 个答案:

答案 0 :(得分:1)

尝试定义没有模块名Services的服务类,如下所示:

#app\services\quiz_data_creation.rb:
class QuizDataCreation
  def initialize(user)
    self.user = user
    logger.debug "[AC] service - initialize user: #{self.user.inspect}"  
  end

  def create
    logger.debug "[AC] Service entered!"  
  end
end

然后,您可以正常访问它:

QuizDataCreation.new(current_user).create