Home > Software design >  Posts per day separator in rails loop
Posts per day separator in rails loop

Time:05-26

I have the following question, in my rails application, I have a page with posts and infinite scroll, it clicks and loads more posts.

What I wanted to implement is that the listing in the view has a separator, even if it's an HR tag, every time the posts are from another day. Because the person can load new posts until they reach posts from other days, and I wanted a separator in the grid every time it arrives at posts from the previous day!

CodePudding user response:

You need a reference to the previous post / post date in the iterator

A couple of options:

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

CodePudding user response:

you need to add post id to each post section then add custom code to the infinite scroll event to pass the last post id so in your backend code you can do Post.where('id > ?', id)

  • Related