Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 铁路服务从未打过电话_Ruby On Rails - Fatal编程技术网

Ruby on rails 铁路服务从未打过电话

Ruby on rails 铁路服务从未打过电话,ruby-on-rails,Ruby On Rails,我已经编写了一个简单的服务来创建我想通过ActionCable连接发送的数据,但从开发日志来看,它似乎从未执行过,我不知道为什么 #app\channels\quiz_data_channel.rb: class QuizDataChannel < ApplicationCable::Channel def subscribed stream_from specific_channel end def send_data logger.debug "[AC]

我已经编写了一个简单的服务来创建我想通过ActionCable连接发送的数据,但从开发日志来看,它似乎从未执行过,我不知道为什么

#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方法有效,输入的第一个文本[AC]send data方法被打印到开发日志中,但仅此而已。我一直在看一些教程,并尝试首先将服务放在模型的子文件夹中,然后调用Services::QuizDataCreation.newuser:current_user.create,但还没有让它工作。我确信有一个非常明显的解决方案,但我就是看不到,所以我真的非常感谢有任何指针。

尝试定义不带模块名的服务类,如下所示:

#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

有道理。我做了您建议的更改,但不幸的是,它没有为我解决问题:您能够从控制器或模型访问服务吗?