2014-01-15 92 views
1

以下是實現長輪詢的代碼。向控制器發送每個請求的長輪詢實施

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 

的問題是所述第一請求被與所述消息

模板丟失 缺失模板你好/索引,應用程序/索引含{立即失敗:區域設置=> [:EN]:格式= > [:html],:handlers => [:erb,:builder,:raw,:ruby,:jbuilder,:coffee]}。搜查:*「/家/ MYHOME的/ tmp /聊天/應用/視圖」

如何延遲渲染,而不是讓軌道搜索視圖文件,而不是返回失敗狀態

回答

0

也許這將工作:

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 
    } 
` 
+0

返回相同的錯誤。沒有變化 –

+0

只是好奇,你有沒有嘗試過在錯誤發生之前立即打印acArray的長度值? – rboling

+0

我放置** p ApplicationController.acArray.length **之前**下一個**在其中打印1錯誤之前的塊 –