1

nanoc を使用してブログ アーカイブ ページを作成し、 http ://daringfireball.net/archive/ に表示されているものと同様のリストを表示したいと考えています。

nanoc のブログ記事の日付の付け方に基づいて、問題が発生しています。私が試したコードは次のとおりです。

by_yearmonth = @site.sorted_articles.group_by{ |a| [a.date.year,a.date.month] }
by_yearmonth.keys.sort.each do |yearmonth|
    articles_this_month = by_yearmonth[yearmonth]
    # code here to display month and year
    articles_this_month.each do |article|
        # code here to display title of blog post
    end
end

nanoc は a.date.year または a.date.month を理解していないようです -- サイトをコンパイルしようとすると、「date」メソッドが定義されていないというエラーが表示されます。

4

2 に答える 2

1

更新: ddfreyne からの重要な指示のおかげで、最終的に機能するようになったコードは次のとおりです。

# In lib/helpers/blogging.rb:
def grouped_articles
  sorted_articles.group_by do |a|
    [ Time.parse(a[:created_at]).year, Time.parse(a[:created_at]).month ]
  end.sort.reverse
end

# In blog archive item:
<% grouped_articles.each do |yearmonth, articles_this_month| %>
    <h2>Year <%= yearmonth.first %>, month <%= yearmonth.last %></h2>
    <% articles_this_month.each do |article| %>
        <h3><%= article[:title] %></h3>
    <% end %>
<% end %>

ありがとう!

于 2012-03-07T22:15:48.143 に答える
0

あなたの質問には質問がありません。:)

あなたはほとんどそこにいます。貼り付けたコードは、記事を年月に正しく分割していると思います。次に、それらを表示する必要があります。ERB または Haml を使用してそれを行うことができます (前者を好む人もいれば、後者を好む人もいます)。たとえば、ERB の場合:

# somewhere in lib/ (I propose lib/helpers/blogging.rb)
require 'date'
def grouped_articles
  sorted_articles.group_by do |a|
    [ Date.parse(a[:date].year, Date.parse(a[:date]).month ]
  end.sort
end

# in your blog archive item
<% grouped_articles.each_pair do |yearmonth, articles_this_month| %>
    <h1>Year <%= yearmonth.first %>, month <%= yearmonth.last %></h1>
    <% articles_this_month.each do |article| %>
        <h2><%= article[:title] %></h2>
    <% end %>
<% end %>

私はそれをテストしていませんが、それが要点です。

于 2012-03-03T09:09:52.250 に答える