【问题标题】:Rails - Reload a partial with javascript intervalRails - 使用javascript间隔重新加载部分
【发布时间】:2016-11-06 22:39:02
【问题描述】:

我正在尝试每隔几秒重新加载包含在 Foundation 选项卡中的部分内容。

apps/views/xrf_cocs/show.html.erb - 这是我最初渲染部分的地方

  <div class="tabs-panel" id="panel6v" style="border-left: solid 1px #f2f2f2">
    <%= render 'lead_reports/index' %>
  </div>

apps/controllers/xrf_cocs_controller.rb - 这是我为部分填充 rails 对象的地方

def show
    @positive_lead_reports = LeadReport.all.to_a
    @positive_lead_reports.delete_if{|cur| cur.xrf_coc_id != @xrf_coc.id}

    @negative_lead_reports = LeadReportNeg.all.to_a
    @negative_lead_reports.delete_if{|cur| cur.xrf_coc_id != @xrf_coc.id}
end

apps/views/lead_reports/_index.html.erb - 这是部分代码的要点

<div id="lead_reports">
    <% @positive_lead_reports.each do |lead_report| %>
       <table>
          <thead>
          </thead>
          <tbody>
          </tbody>
       </table>
    <% end %>
       ... repeat above for negative reports
</div>

基本上,我正在尝试在我的apps/views/xrf_cocs/show.html.erb 文件中添加一个 jQuery 函数,该函数将每 3 秒调用一次 render 'lead_reports/index'

任何人都可以提供一些帮助吗?

【问题讨论】:

    标签: javascript jquery ruby-on-rails ajax


    【解决方案1】:

    render 是一种 Ruby on Rails 方法。因此,我非常怀疑您是否可以直接调用它。刷新局部视图的最佳方法可能是使用 ajax 请求。

    Is it possible to refresh partial frequently using Ajax?

    【讨论】:

    • AJAX 仅在有表单提交时才有效。这不是提交任何东西,因为没有发送我也可以响应的 ajax 请求。
    • AJAX 与表单提交无关。它只是一种传输数据的方式。您可以使用 setInterval 简单地触发 ajax 请求。请检查上面的链接。
    猜你喜欢
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多