我在获取一组数据以在 ruby 中分页时遇到问题。我已经有一个示例(您正在“关注”的用户的帖子新闻源)
_feed_item.html.erb
<li id="<%= feed_item.id %>">
<%= link_to gravatar_for(feed_item.user), feed_item.user %>
<span class="user">
<%= link_to feed_item.user.name, feed_item.user %>
</span>
<span class="content"><%= feed_item.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
</span>
# not part of the code - just a placeholder for a like button I'm writing
<span class="likebtn-wrapper" data-lang="en"></span>
##########################
<% if current_user?(feed_item.user) %>
<%= link_to "delete", feed_item, method: :delete,
data: { confirm: "You sure?" },
title: feed_item.content %>
<% end %>
</li>
_feed.html.erb
<% if @feed_items.any? %>
<ol class="microposts">
<%= render partial: 'shared/feed_item', collection: @feed_items %>
</ol>
<%= will_paginate @feed_items %>
<% else %>
home.html.erb(呈现新闻源)
<%= render 'shared/feed' %>
因为默认主页仅呈现您关注的用户的帖子 - 我创建了一个“全局”帖子索引,它简单地列出了保存到数据库中的所有帖子。
全球.html.erb
Global Feed |
<%= User.count %> Total Users |
<%= Micropost.count %> Total Posts<br />
<% Micropost.all.each do |micropost| %>
Global Post Number: <%= micropost.id %> <br />
<%= link_to micropost.user.name, micropost.user %> <br />
<%= micropost.content %><br />
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<% end %>
我尝试使用与新闻源相同的技术,但它不起作用。我将如何对类似于用户新闻源的结果进行分页。