简体   繁体   English

在带有Rails的页面上显示下一组结果,例如Facebook风格的“旧帖子”链接,以查看下一组新闻

[英]Show next set of results in page with Rails, like Facebook-style “older posts” link to see next set of news items

I'm still pretty new to Rails and need your help: I have been creating a social fitness analytics site (zednine.com) that has an activity stream that lists workouts posted on the site. 我对Rails还是很陌生,需要您的帮助:我一直在创建一个社交健身分析网站(zednine.com),该网站的活动流列出了发布在该网站上的锻炼。 Several pages currently show the 10 most recently updated workouts. 当前有几页显示了10个最近更新的锻炼。 I'd like to add a link at the bottom to "Older workouts." 我想在底部添加一个指向“旧锻炼”的链接。 On click, this should show the next 10 workouts in the page, immediately below the first 10, with a new link to Older below (now 20 displayed in the page) -- just like the news stream on Facebook and several other social networks. 在单击时,这应显示页面中接下来的10个锻炼,紧接在前10个锻炼之后,并在下方显示指向“较旧”的新链接(现在在页面中显示20个锻炼),就像Facebook和其他几个社交网络上的新闻流一样。

What I've tried so far: 到目前为止,我已经尝试过:

  1. I'm currently using a find with :limit to get the first N results 我当前正在使用带有:limit的查找来获取前N个结果
  2. I can set up a unique find with :limit and :offset for each set of N results with hidden divs, but that's lame and does not extend well 我可以为每个带有隐藏div的N个结果集设置一个:find和:offset的唯一查找,但这很la脚,而且扩展性不佳

I also looked at: 我还看了看:

  1. pagination, including will_paginate, but not clear whether this can help for in same page chunking? 分页,包括will_paginate,但不清楚是否可以在同一页面分块中提供帮助?
  2. collections...? 收藏...?

What is the right/a good way to do this? 什么是正确/好的方法?

Also, how can I include records from multiple tables in this sort of stream? 另外,如何在这种流中包含来自多个表的记录? Eg, list could include workouts from one table, journal entries from another, comments from a third, all intermixed and sorted by date? 例如,列表可以包括一个表中的锻炼,另一个表中的日记条目,第三个表中的注释,所有注释都按日期混合并排序?

Thank you! 谢谢!

Will_paginate will do the job, just pass in the page you want: Will_paginate会完成这项工作,只需传递您想要的页面即可:

<%= link_to "Older Posts", model_route_path(:page => next_page) %>

As for the second question, simply create a Feeds model (or tack it onto an existing one). 至于第二个问题,只需创建一个Feed模型(或将其添加到现有模型上)。 Then have a method which fetches recent entries from the various other models and sorts them by created_at date. 然后有一个方法可以从其他各种模型中获取最新条目,并按created_at日期对它们进行排序。 I would probably implement #recent method on each of the models and call that in your Feed object. 我可能会在每个模型上实现#recent方法,并在Feed对象中调用它。

Models:

class Feed
  def index
    entries = []
    entries << Journal.recent
    entries << Comment.recent
    # etc
    entries.sort_by {|entry| entry['created_at']}
  end
end

class Journal < ActiveRecord::Base
  def recent
    self.find_all_by_created_at(:limit => 10)
  end
end

Or something like that. 或类似的东西。 You will have to be very careful about scalability here. 您在这里必须非常注意可伸缩性。

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

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