简体   繁体   English

Rails渲染部分两次

[英]Rails renders partial twice

Rails is outputting my articles partial twice. Rails输出我的文章两次。 Currently, I just loop through the articles collection and output it. 目前,我只是循环文章集合并输出它。 At first I believed it was a problem with AJAX but I deleted all of the javascript in the application.js and the index.js.erb files and it still repeats twice. 起初我认为它是AJAX的一个问题,但我删除了application.js和index.js.erb文件中的所有javascript,它仍然重复两次。 I've also checked the database by logging in through Rails c and running Articles.all.count and that returns the correct # of articles. 我还通过登录Rails c并运行Articles.all.count来检查数据库,并返回正确的文章数。 I've also reset the db and tried it again with the same result. 我也重置了数据库并再次尝试使用相同的结果。

article#index 文章#指数

<h1 class="text-center">talks</h1>

<div id="articles-list" class="small-12 small-centered columns">
  <%= render @articles %>
</div>  

<%= will_paginate @articles %>

_article.html.erb partial: _article.html.erb partial:

<% @articles.each_with_index do |article, index| %>
<%= index %>
<dl class="individual-article">
    <dt><%= article.title %> 
    <% if current_user.try(:admin) %>
        | <%= link_to 'Edit', edit_article_path(article) %>
    <% end %><br> 
    <%= raw article.tags.map(&:name).map { |t| link_to t, tag_path(t), class: get_class(t) }.join(', ') %>
    </dt>
    <dd><%= truncate(article.body.html_safe, length: 200) %>
        <%= link_to 'more', article_path(article) %>
    </dd>
</dl>

I put the index on to capture if it's repeating and it is. 我把索引打开,如果它正在重复,它就是。 If I have three articles it will output 0 1 2 0 1 2 for the indeces. 如果我有三篇文章,它将输出0 1 2 0 1 2作为indeces。 So I know it's getting executed twice. 所以我知道它会被执行两次。

article controller 物品管理员

def index
    if params[:tag]
        @articles = Article.tagged_with(params[:tag]).order('created_at DESC').paginate(page: params[:page], per_page: 3)
    else 
        @articles = Article.order('created_at DESC').paginate(page: params[:page], per_page: 3)
    end
end

I have some AJAX for infinite scrolling which may be the problem. 我有一些AJAX用于无限滚动,这可能是问题所在。 Except if I delete the entire js file, it has the same behavior. 除非我删除整个js文件,否则它具有相同的行为。

index.js.erb index.js.erb的

$('#articles-list').append('<%= escape_javascript render(@articles) %>');
$('.pagination').replaceWith('<%= escape_javascript will_paginate(@articles) %>');

Application.js 的application.js

$(document).ready(function() {
  if ($('.pagination').length) {
    $(window).scroll(function() {
      var url = $('.pagination .next_page').attr('href');
      if (url && $(window).scrollTop() > $(document).height() - $(window).height() - 50) {
        $('.pagination').text("Please Wait...");
        return $.getScript(url);
      }
    });
    return $(window).scroll();

  }
});

I'm relatively new to Rails and would appreciate advice on how to prevent this from executing twice. 我对Rails相对较新,并希望了解如何防止这种情况两次执行。

Edit 编辑

Partial server logs: 部分服务器日志:

...
Article Load (0.2ms)  SELECT  "articles".* FROM "articles"  ORDER BY created_at DESC LIMIT 3 OFFSET 0
...
Rendered articles/_article.html.erb (6.5ms)
Rendered articles/index.html.erb within layouts/application (12.8ms)
Rendered layouts/_header.html.erb (0.4ms)

You are passing a collection to the partial. 您正在将集合传递给partial。 This will render the partial one time for each element in @articles. 这将为@articles中的每个元素渲染部分时间。 In the partial you then loop again on @articles. 在部分中,然后再次在@articles上循环。 Take the loop outside the partial or delete it if you don't need the index. 如果您不需要索引,请将该循环置于partial之外或删除它。 These will both work: 这些都有效:

article#index 文章#指数

<div id="articles-list" class="small-12 small-centered columns">
  <% @articles.each_with_index do |article, index| %>  
     <%= render 'article', article: article, index: index %>
  <%end%>
</div>

_article.html.erb partial: _article.html.erb partial:

<%= index %>
<dl class="individual-article">
  <dt><%= article.title %> 
    <% if current_user.try(:admin) %>
      | <%= link_to 'Edit', edit_article_path(article) %>
    <% end %><br> 
    <%= raw article.tags.map(&:name).map { |t| link_to t, tag_path(t), class: get_class(t) }.join(', ') %>
  </dt>
  <dd><%= truncate(article.body.html_safe, length: 200) %>
    <%= link_to 'more', article_path(article) %>
  </dd>
</dl>

or if you don't need the index in the partial, keep index.html as it is right now and just remove the loop from the partial: 或者如果你不需要partial中的索引,请保持index.html,就像现在一样,只需从partial中删除循环:

_article.html.erb partial: _article.html.erb partial:

<dl class="individual-article">
  <dt><%= article.title %> 
    <% if current_user.try(:admin) %>
      | <%= link_to 'Edit', edit_article_path(article) %>
   <% end %><br> 
   <%= raw article.tags.map(&:name).map { |t| link_to t, tag_path(t), class: get_class(t) }.join(', ') %>
  </dt>
  <dd><%= truncate(article.body.html_safe, length: 200) %>
    <%= link_to 'more', article_path(article) %>
  </dd>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM