RAILS自动刷新部分 [英] RAILS Auto refresh partial

查看:133
本文介绍了RAILS自动刷新部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有动作显示的控制器事件:

I have a controller events with action show :

  def show
    @event = Event.get(params[:id])

    if !connected? || !@event 
      return redirect_to request.referrer || root_path
    end

    @requests = @event.get_requests
  end

在我有 show.html.erb 的视图中

<script>
  $(document).ready(
     function() {
      setInterval(function() {
        $('#requests').load('/events/reload_requests');
    }, 2000);
  });
</script>
...
<div class="party-block" id="requests">
    <%= render partial: 'requests' %>
</div>
...

javascript函数调用控制器方法 events/reload_requests :

The javascript function call controller method events/reload_requests :

def reload_requests
  @requests = @event.get_requests
  render partial: 'events/requests'
end

以及部分 _requests.html.erb 的内容:

<%= Time.now %>
<h4><%= t '.requests' %></h4>
<% @requests.each do |r| %>
 <span class="request">
 ...
<% end %>

问题:javascript函数将div #requests 中的 show.html.erb 页面的所有内容加载到 show.html的副本中.erb 在div中,它会正确刷新div #request: 在此处输入图像描述 您可以看到,在show#requests中,我具有show的内容. 比你

Problem : The javascript function load all the content of my show.html.erb page in div #requests, but in the copy of show.html.erb in the div, it refresh div #request correctly : enter image description here You can see, in show#requests i have the content of show. Than you

推荐答案

重新阅读您的问题之后,我认为我理解了您的问题.

After re-reading your question, I think I understood your problem.

当调用#load方法时,返回的HTML实际上是整个show.html.erb模板..该模板被插入到$('#requests')元素中.正确吗?

When you call the #load method, the HTML returned is actually your whole show.html.erb template.. which gets inserted into your $('#requests') element. Is that correct?

在jQuery文档中,您可以在以下位置找到以下内容: http://api.jquery.com/load /

In the jQuery docs, you can find the following at: http://api.jquery.com/load/

加载页面片段

Loading Page Fragments

.load()方法与$ .get()不同,它允许我们指定 要插入的远程文档. (...)

The .load() method, unlike $.get(), allows us to specify a portion of the remote document to be inserted. (...)

$("#result").load("ajax/test.html #fragment");

$( "#result" ).load( "ajax/test.html #fragment" );

也许您可以按照以下步骤修改脚本标签:

Maybe if you modify your script tag as follow:

<script>
  ...
        $('#requests').load('/events/reload_requests #requests');
  ...
</script>

您只能插入您感兴趣的HTML片段.

you can insert only the fragment of the HTML you are interested in.

请问这是否不是您的问题.

Please let me know if that was not your question.

此外,如果您对'event/reload_requests'的请求返回了show.html.erb模板,则您可能会错误地将event/reload_requests端点路由到您的#show操作.

Also, if your request to 'event/reload_requests' is returning your show.html.erb template, you may be mistakenly routing the event/reload_requests endpoint to your #show action.

编辑#1

解决嵌套div问题的最干净方法似乎是添加一个容器div并从那里加载:

The cleanest way to fix the nested div problem seems to be to add a container div and load from there:

$('#requestsContainer').load('/events/reload_requests #requests');

html:

<div id="requestsContainer">
  <div class="party-block" id="requests">
      <%= render partial: 'requests' %>
  </div>
</div>

这篇关于RAILS自动刷新部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆