I'm trying to archive the articles on my rails blog by month or what have you. Just some sort of order.
I currently have this in my archives controller
class ArchivesController < ApplicationController
def index
@arcticles = Article.all.group_by(:select => "title, id, created_at", :order = "created_at DESC")
@articles_month = @arcticles.group_by { |t| t.due_at.month}
end
end
And here is my view. I tried going off a railscast episode but feel that maybe its a bit too old and I'm off here. Thoughts anyone? Thanks.
<div class="archives">
<% @articles_month.each do |month, articles| %>
<h2><%= month.strftime('%B') %></h2>
<% for article in articles %>
<strong><%= article.name %></strong>
due on <%= article.due_at.to_date.to_s(:long) %>
</div>
<% end %>
<% end %>
I'm pretty sure I figured it out.
Did this in my articles controller
def index
@article = Article.all
@articles = Article.find(:all, :order => 'created_at DESC')
@articles_by_month = Article.find(:all, :order => 'created_at DESC').group_by { |article| article.created_at.strftime("%B %Y") }
end
view
<h2>Archives</h2>
<% @articles_by_month.each do |monthname, articles| %>
<h3="month-archive"><%= monthname %></h3>
<ul id="archive">
<% articles.each do |article| %>
<li class="archive-post"><%= link_to article.title, article_path(article) %></li>
<% end %>
<% end %>
</ul>