如何将Blog系统的存档菜单实施到rails3 app中? [英] 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???
需要您的帮助!
推荐答案
<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")
@posts
是array
,因此将对其中的项目进行排序,然后我可以根据其顺序获取记录.
The @posts
is an array
so the items inside it ll be ordered and then i can fetch the records according to it ordering.
谢谢.
这篇关于如何将Blog系统的存档菜单实施到rails3 app中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!