Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 对控制器的每个请求执行长轮询

Ruby on rails 对控制器的每个请求执行长轮询,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,下面是实现长轮询的代码 class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception cattr_accessor :acArray end Appl

下面是实现长轮询的代码

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  cattr_accessor :acArray
end

ApplicationController.acArray = []

class HelloController < ApplicationController
  def initialize
    ApplicationController.acArray << self
  end

  def index
    ApplicationController.acArray.each_with_index {|val, index|
      if index == 1 # only on second request serve the first request, until then hold the object in memory
          val.render :text => ApplicationController.acArray.length
      end
    }
  end
end
class ApplicationController
问题是第一个请求立即失败,并显示消息

缺少模板 缺少模板hello/index,带有{:locale=>[:en],:formats=>[:html],:handlers=>[:erb,:builder,:raw,:ruby,:jbuilder,:coffee]}的应用程序/索引。在以下位置搜索:“//主页/myhome/tmp/chat/app/views”

如何延迟渲染,不让rails搜索视图文件,也不返回失败状态

也许这样可以:

    until ApplicationController.acArray.length > 1 do |process|
    end
    ApplicationController.acArray.each_with_index{|val, index|
        if index == 1
            val.render :text => ApplicationController.acArray.length
        end 
    }
`

好奇的是,您是否尝试在错误发生之前立即打印acArray的长度值?我将p ApplicationController.acArray.length before next放在else块中,该块在错误之前打印1。这将阻止服务器。也直到不采取过程参数。