【问题标题】:ruby hash calling through javascript通过 javascript 调用 ruby​​ 哈希
【发布时间】:2013-01-17 12:40:40
【问题描述】:

以下文件在 .html.erb 中看到第二次调用它需要旧的。

    <%

        mash = {
          "id" => id, "legends" => legends, "x_units" => x_units, "y_units" => y_units, "data" => data, "alertPoints" => alert_points, "time_mode" => time_mode, 
          "colors" => colors, "x_label" => x_label, "y_label" => y_label, "tipColors" => tipColors, "dateformat" => @dateformat, "timeformat" => @timeformat, 
          "chart_url" => chart_url, "x_label_date_format" => x_label_date_format, "turn_series" => turn_series, "y_ticks_array" => y_ticks_array, "y_label_width" => y_label_width,
          "series_type" => series_type,"vertical_threshold" => vertical_threshold, "x_label_suffix" => x_label_suffix, "y_tick_on" => y_tick_on, "xmin" => xmin, "xmax" => xmax
          }
        %>


            <script type="javascript">

                  setInterval(    function ()
                 {
                 onDataReceived(<%=mash.to_json.html_safe%>);



                 }, 5000);
              </script>

【问题讨论】:

  • 您是如何获取数据的?你的ajax回调是什么样的?如果没有更多细节,有点难以分辨出了什么问题......
  • 如果使用 put 命令查看 mash[data] 它会动态变化。但是 它会变得旧值。我可以一次又一次地调用上述 mash

标签: ruby


【解决方案1】:

问题是 mash 在页面渲染时被评估一次,所以一次又一次地使用 erb 总是会产生相同的结果。如果您需要新数据,您可能需要使用 AJAX 请求它。

In Rails, how do you render JSON using a view?

使用 jQuery 进行 ajax 调用的文档:http://api.jquery.com/jQuery.ajax/

例如:

setInterval(function() { 
  $.get('route/to/mash.json', function(data) {
     onDataReceived(data);
  });
}, 5000);

【讨论】:

    猜你喜欢
    • 2014-03-03
    • 2011-07-22
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    • 2011-01-15
    相关资源
    最近更新 更多