2

ブログページに行くと、メニューにアーカイブ一覧が表示されます。ほとんどの場合、次のように表示されます

'Archive'

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

scaffold を使ってブログ投稿システムを作る自信があります。しかし、私はこのアーカイブを作成する方法がわかりません:(これをアプリに簡単に実装するための良いアイデアを思いついた人はいますか???

君の力が必要!!

4

1 に答える 1

1
<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 %>

これはあなたを助けるかもしれません。このコードにはカウント数を含めていません。実際にそれを行う方法を考えています。教えて頂ければ。

また、コントローラではこれを行いました。

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

これ@postsは、arrayその中のアイテムが順序付けられるので、その順序に従ってレコードを取得できます。

ありがとう。

于 2013-06-20T20:14:50.153 に答える