2012-10-27 36 views
2

嗨,我是一個相當新手在Rails和幾天在紅寶石exp。我使用線程創建了一個秒錶程序。但現在我想把它放入我的Rails應用程序中,並且視圖應該有按鈕來啓動,停止和恢復秒錶,因爲我正在跟蹤一個事件。有人可以顯示如何將紅寶石程序嵌入到rails中並從視圖中使用它?如何在rails3中嵌入用戶創建的ruby程序

以下程序應當從開始/停止/恢復按鈕視圖被調用

stopwatch.rb:(它只是開始方法和暫停方法截至目前)

class Stopwatch 

def initialize 
    @t_start 
    @t_elapsed 
    @t_total 
    @t_current 
end 


def start 
    @t_start = Time.now 
    t = Thread.new do 
      while true do 
      @t_current = Time.now 
      puts @t_current 
      @t_elapsed = @t_current - @t_start 
      puts "Time elapsed is : #{@t_elapsed.to_i} seconds" 
      sleep 1 
      end 

    end 

end 

def pause 
    # Stop the Thread => Sleep the Thread until Resume is pressed. 
    t.kill 
    # Calculate the elapsed time 
     @t_elapsed = @t_current - @t_start 

    end 

end 

ticker = Stopwatch.new 

ticker.start 

謝謝,

回答

0

所以首先你可以在你的應用程序的lib/目錄中添加該類。 然後問題是你不能直接向視圖puts東西。一種解決方案可能是使用Pusher。然後在你的控制器中,你可以觸發(doc & examples there)你想發送到視圖的數據。

然後,您將獲得javascript中的值,並且您可以更新視圖。

爲了讓按鈕起作用,您還需要使用Ajax查詢來完成此操作,並在您的控制器中處理該按鈕。

相關問題