【问题标题】:how to embed a user created ruby program in rails3如何在 rails3 中嵌入用户创建的 ruby​​ 程序
【发布时间】:2012-10-27 22:49:51
【问题描述】:

您好,我是 Rails 的新手,并且在 ruby​​ 方面有几天经验。我使用线程创建了一个秒表程序。但是现在我想把它放在我的 Rails 应用程序中,并且视图应该有按钮来开始、停止、恢复秒表,因为我正在跟踪一个事件。有人可以展示如何将 ruby​​ 程序嵌入到 rails 并从视图中使用它吗?

下面的程序应该从视图中的开始/停止/恢复按钮调用

stopwatch.rb:(目前只有start方法和pause方法)

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

谢谢,

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3


    【解决方案1】:

    所以首先你可以在你的应用程序的lib/ 目录中添加这个类。 那么问题是你不能puts东西直接到视图。一种解决方案可能是为此使用Pusher。然后在您的控制器中,您可以触发 (doc & examples there) 您想要发送到视图的数据。

    然后您将在 javascript 中获取该值,然后您可以更新视图。

    为了让按钮工作,您还需要使用 Ajax 查询并在控制器中处理它

    【讨论】:

      猜你喜欢
      • 2011-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-22
      • 2021-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多