gpt4 book ai didi

ruby-on-rails - Rails 渲染部分两次

转载 作者:数据小太阳 更新时间:2023-10-29 07:47:45 25 4
gpt4 key购买 nike

Rails 两次输出我的部分文章。目前,我只是循环遍历文章集合并输出它。起初我认为这是 AJAX 的问题,但我删除了 application.js 和 index.js.erb 文件中的所有 javascript,它仍然重复两次。我还检查了数据库,方法是通过 Rails c 登录并运行 Articles.all.count 并返回正确的文章数。我还重置了数据库并再次尝试,结果相同。

文章#索引

<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 部分:

<% @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>

我把索引放在上面以捕获它是否重复,确实如此。如果我有三篇文章,它将为索引输出 0 1 2 0 1 2。所以我知道它被执行了两次。

文章 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

我有一些用于无限滚动的 AJAX,这可能是问题所在。除非我删除整个 js 文件,否则它具有相同的行为。

index.js.erb

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

应用程序.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();

}
});

我对 Rails 比较陌生,希望得到有关如何防止它执行两次的建议。

编辑

部分服务器日志:

...
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)

最佳答案

您正在将一个集合传递给部分。这将为@articles 中的每个元素呈现一次部分。在部分中,您然后再次循环@articles。如果不需要索引,则将循环放在部分之外或将其删除。这些都有效:

文章#索引

<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 部分:

<%= 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>

或者如果您不需要部分中的索引,请保持 index.html 不变,并从部分中删除循环:

_article.html.erb 部分:

<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>

关于ruby-on-rails - Rails 渲染部分两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31483424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com