1) Replace line 5 of the application / controllers / items_controller.rb with:
@items = Item.all(:order => "date DESC")
2) Replace line 3-14 of the /views/items/index.html.erb application with:
<% date = 1.day.from_now(@items.first.date.to_date) rescue nil # increment the day
@items.each do |item|
if date > item.date.to_date
date = item.date.to_date %>
<div style="background: #81BEF7;margin-top: 5px; margin-bottom: 5px;" class="rounded"><b><span style="padding: 5px"><%=h date %></span></b></div>
<%end%>
<p>
<i><%=h item.time %> - </i>
<%= link_to "#{item.title}", item.link %><br>
<a href="/items/<%=item.id%>"><span class="subtext" style="margin-left: 55px">by <%= item.user.username %> | <%=item.comments.count%> comments </span></a>
</p>
<% end # end for items_each%>
In this approach, you use a database for sorting and a simple comparison for grouping.
PS: I don’t think it’s a good idea to name the columns of the database “date”. Some of the date databases have a keyword reserved.
source
share