【问题标题】:Ruby on Rails PaginationRuby on Rails 分页
【发布时间】:2013-06-17 02:25:14
【问题描述】:

我在获取一组数据以在 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' %>

因为默认主页仅呈现您关注的用户的帖子 - 我创建了一个“全局”帖子索引,它仅列出保存到数据库中的所有帖子。

global.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 %>

我尝试使用与新闻源相同的技术,但它不起作用。我将如何对类似于用户新闻源的结果进行分页。

【问题讨论】:

    标签: ruby-on-rails ruby pagination


    【解决方案1】:

    你不应该使用

    <% Micropost.all.each do |micropost| %>
    

    在您的 global.html.erb 文件中。

    改为在此文件中使用&lt;%= render @feeds %&gt;。它将使用 _feed.html.erb 处理每个提要

    在你的 microposts_controller.rb 你应该有这样的东西:

      def index
        @feeds = Micropost.paginate(page: params[:page])
      end
    

    希望对您有所帮助。

    【讨论】:

    • 产生错误 C:/Sites/NFE/app/views/static_pages/global.html.erb:28: syntax error, unexpected keyword_ensure, expecting $end _feed.html 只抓取您关注的用户的帖子。即使您没有关注该人,全局页面也需要显示所有帖子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-23
    • 2015-08-13
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多