Rails will_paginate无尽的滚动,带有一个删除前 3 个项目的数组



我有一个部分,我想删除或不显示数组中的前三篇文章,因为它们位于特色文章部分。我还希望部分使用带有无休止滚动的will_paginate来加载下一页文章。我面临的问题是,当使用@articles.drop(3).each do |a|并且下一页加载时,数组会再次删除接下来的三篇文章。

解决这个问题的最佳方法是什么?我最初的想法是数组中的数组,其中第一个数组删除前 3 个,然后嵌套数组返回所有文章,但我不确定该怎么做?

部分数组代码:

<% @articles.drop(3).each do |a| %>
<%= link_to a.source_url, :class => "flexRow" do %>
<%= a.source %>
<h3><%= a.title %></h3>
<% end %>
<% end %>

索引.js.erb

$('#article-index').append(' <%= j render("articles") %>');
<% if  @articles .next_page %>
$('.pagination').replaceWith('<%= j will_paginate(@articles, :previous_label => '', :next_label => '', :page_links => false) %>');
<% else %>
$('.pagination').remove();
<% end %>

索引.html.erb

<div id="article-index">
<%= render 'articles' %>
</div>

更新 这个解决方案似乎有效,但感觉不优雅?

<% (@articles.current_page == 1 ? @articles.drop(3) : @articles).each do |a| %>

试试

@articles[3..@articles.count]

这将删除保存在索引 0、1 和 2 处的记录,并返回剩余的记录。

您可以在控制器中执行以下操作:

@articles = Article.where(...).paginate(page: params[:page], per_page: 10)
# Works only for the first HTML request
unless request.xhr?
@articles.shift(3)
end
.
.
.
respond_to do |format|
format.html
format.js
end

现在,当您遍历@articles时,它将从索引 3 开始,只是第一次。

编辑

<% (request.xhr? ? @articles : @articles[3..10]).each do |a| %>
<%= link_to a.source_url, :class => "flexRow" do %>
<%= a.source %>
<h3><%= a.title %></h3>
<% end %>
<% end %>

假设页面大小为 10。

最新更新