1

ブログエントリのすべての月を一覧表示するブログアーカイブのサイドバーを作成しようとしているので、「2007年6月」などのリンクをクリックすると、6月7日以降のすべてのブログが読み込まれます。これが私のlink_toです

<%= link_to month.first.strftime("%B %Y"), blog_archive_month_path(month.first.strftime("%Y-%m-%d")) %>

month.firstは私が引き上げたレコードです。私のコントローラーは次のようになりますか?:

@blog_posts = BlogPost.where(:created_at.strftime("%Y-%m-%d") => params[:date]).(:select => "title, id, slug, created_at", :order => "created_at DESC")

レコードのcreated_byフィールドを、一致で渡すことができる形式に変換できることを望んでいましたが、未定義のメソッドエラーが発生します

4

2 に答える 2

3

これはどう?

リンクを1年月だけに下げます。

<%= link_to month.first.strftime("%B %Y"), blog_archive_month_path(:date => month.first.strftime("%Y-%m")) %>

次に、範囲構文を使用して、次のSQLを取得します。

@blog_posts = BlogPost.
              where(:created_at => (params[:date].to_date..(params[:date].to_date + 1.month))).
              order("created_at desc")
于 2011-06-07T03:32:12.167 に答える
0

基本的に私はダン・クロークが言っていることに同意します(+1)。彼の答えの唯一の間違いは、 (彼の例のように).to_date完全な日付文字列がない場合にエラーをスローすることです。params[:date]したがって、私の提案は次のようになります。

意見:

<%= link_to month.first.strftime("%B %Y"), blog_archive_month_path(month.first.strftime("%Y-%m-%d")) %>

コントローラ:

@blog_posts = BlogPost.
  where(:created_at => params[:date].to_date.beginning_of_month..params[:date].to_date.end_of_month).
  order("created_at desc")

元のコードの問題は、を呼び出そうとしていることですがstrftime:created_atこれは不可能です。

または、URLの完全な日付が気に入らない場合は、次のようにすることができます。

<%= link_to month.first.strftime("%B %Y"), blog_archive_month_path(month.first.strftime("%Y-%m")) %>

と:

@blog_posts = BlogPost.
  where(:created_at => "#{params[:date]}-01".to_date.beginning_of_month.."#{params[:date]}-01".to_date.end_of_month).
  order("created_at desc")
于 2011-06-07T15:29:42.700 に答える