rails循环中的每日Posts分隔符



我有以下问题,在我的rails应用程序中,我有一个带有帖子和无限滚动的页面,它点击并加载更多的帖子。

我想实现的是,视图中的列表有一个分隔符,即使它是一个HR标签,每次帖子都来自另一天。因为这个人可以加载新的帖子,直到他们到达其他日子的帖子,我想在网格中有一个分隔符,每次它到达前一天的帖子!

您需要在迭代器

中引用上一个post/post日期几个选项:

posts.each_with_index do |post, index|
add_separator if index.positive? && post.date != posts[index-1].date
end
prev_date = nil
posts.each do |post|
add_separator if prev_date && post.date != prev_date
prev_date = post.date
end

您需要为每个帖子部分添加帖子id然后向无限滚动事件添加自定义代码,以传递最后一个post id在后端代码中,你可以输入Post.where('id > ?', id)

最新更新