HUSTEN
HUSTEN

Reputation: 5197

How do you implement archive menu for blog system into rails3 app?

When you go to blog page, you will see the archive list on the menu. In most cases, it shows something like this

'Archive'

2012(78)
 -December(1)
 -November(5)
 -October(10)
 ...
2011(215)
2010(365)

I'm confident to make blog posting system by using scaffold. But I have no idea how to make this Archive:( Anyone come up with good idea to implement this to app easily???

Need your help!!

Upvotes: 2

Views: 501

Answers (1)

Ahmad hamza
Ahmad hamza

Reputation: 1936

<h3>Archives </h3>
<% if @posts.to_a.empty? %>
<div class="post">
    <p>No articles found...</p>
</div>
<% else %>
<% current_month = 0 %>  
<% current_year = 0 %>
<% for article in @posts %> 
  <% if (article.created_at.year != current_year)
     current_year = article.created_at.year
   %>
      <h3 class="archiveyear"><%= article.created_at.year%></h3>
<% end %>
<% if (article.created_at.month != current_month || article.created_at.year != current_year) 
 current_month = article.created_at.month 
 current_year = article.created_at.year
%>  

<h4 class="archivemonth"><%= (Date::MONTHNAMES[article.created_at.month]) %></h4>
<% end %>
<div class="archivepost">
<%= link_to article.title, article_path(article), :remote => true %> on <%= article.created_at.strftime('%A')%> - <%= article.created_at.strftime('%d') + "th"%>
</div>
<% end -%>
<%end %>

This may help you. I ve not included the number of counts in this code. Actually m figuring how to do it. If u can let me know.

Also in the controller ive done this.

@posts = Article.order("created_at DESC")

The @posts is an array so the items inside it ll be ordered and then i can fetch the records according to it ordering.

Thanks.

Upvotes: 1

Related Questions