我在网上搜索过所有内容,但都没有找到答案。我试图为我的博客基于日期有一个非常标准的归档选项。对网址blog.com/archive/2009的请求显示了2009年的所有帖子,blog.com/archive/2009/11显示了2009年11月的所有帖子等。我发现了两种不同的代码,但对我没有太大帮助。在Rails中按日期对博客文章进行分组和排序
def display_by_date
year = params[:year]
month = params[:month]
day = params[:day]
day = '0'+day if day && day.size == 1
@day = day
if (year && month && day)
render(:template => "blog/#{year}/#{month}/#{date}")
elsif year
render(:template => "blog/#{year}/list")
end
end
def archive
year = params[:year]
month = params[:month]
day = params[:day]
day = '0'+day if day && day.size == 1
if (year && month && day)
@posts_by_month = Blog.find(:all, :conditions => ["year is?", year])
else
@posts_by_month = Blog.find(:all).group_by { |post| post.created_at.strftime("%B") }
end
end
任何帮助表示赞赏。
什么是你的问题?一般如何做这样的事情?这两个片段如何工作? – gregor 2010-04-28 07:59:18
没有这两个片段只是我试图让它工作。我的问题是我怎样才能按日期排列博客帖子。例如像这个网站http://railstips.org/blog/archives/2010/04/18/。如果你删除18,那么你看到所有发表的帖子,当你删除04,你会看到所有帖子在2010年 – 2010-04-28 12:31:42