How do you implement the archive menu for a blog system in a rails3 application?

When you go to the blog page, you will see a list of archives in the menu. In most cases, it shows something like this

'Archive'

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

I am sure you are making a blog system using scaffold. But I have no idea how to make this Archive :( Has anyone come up with a good idea to implement this application easily ???

You need your help!

+2
source share
1 answer
<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 can help you. I did not include the number of samples in this code. In fact, pondering how to do this. If you can let me know.

Also in the controller I did this.

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

@posts - array, , .

.

+1

Source: https://habr.com/ru/post/1543339/


All Articles