2010-04-28 65 views
1

我在网上搜索过所有内容,但都没有找到答案。我试图为我的博客基于日期有一个非常标准的归档选项。对网址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 

任何帮助表示赞赏。

+1

什么是你的问题?一般如何做这样的事情?这两个片段如何工作? – gregor 2010-04-28 07:59:18

+0

没有这两个片段只是我试图让它工作。我的问题是我怎样才能按日期排列博客帖子。例如像这个网站http://railstips.org/blog/archives/2010/04/18/。如果你删除18,那么你看到所有发表的帖子,当你删除04,你会看到所有帖子在2010年 – 2010-04-28 12:31:42

回答

0

编辑:我设法让它工作。如果你去“博客/ 2010”你看到在2010年发表的文章,如果你去“博客/ 2010/APR”你看到在2010年4月发表的文章等

def archive 
year = params[:year] 
month = params[:month] 
day = params[:day] 
if (year && month && day) 
    requested_date = Date.new(year.to_i, Date.parse(month).month.to_i, day.to_i) 
    from = requested_date - 1 
    to = requested_date + 1 
    @posts_by_month = Blog.find(:all, :conditions => ["due BETWEEN ? AND ?", from, to]) 
elsif (year && month) 
    requested_month = Date.new(year.to_i, Date.parse(month).month.to_i) 
    end_month = requested_month.end_of_month 
    @posts_by_month = Blog.find(:all, :conditions => ["due BETWEEN ? AND ?", requested_month, end_month]) 
else 
    requested_year = Date.new(year.to_i) 
    @posts_by_month = Blog.find(:all, :conditions => ["created_at BETWEEN ? AND ?", requested_year, requested_year.end_of_year ]) 
end 
end 

#routes.rb 
map.connect 'blog/:year/:month/:day', 
:controller => 'blogs', 
:action => 'archive', 
:year => /\d{4}/, 
:month => /\w{3}/, 
:day => /\d{2}/, 
:day => nil, 
:month => nil 

我不知道这是不错的代码,但我相信有人可以做得更好。如果有人这样做,我会很感激。

+0

这似乎是另一个问题,而不是答案... :) – 2010-04-28 14:47:29

+0

大声笑你说得对。我试图弄清楚,我会在完成后更新这篇文章。 – 2010-04-28 15:35:36

0

我更新了发送hil的Rails 4的答案。与他一样,我不知道这是否是“好”的代码,但它似乎对我有用。如果我找到/做任何改进,我会更新这篇文章。

def archive 
    year = params[:year] 
    month = params[:month] 
    day = params[:day] 

    if (year && month && day) 
     requested_date = Date.new(year.to_i, Date.parse(month).month.to_i, day.to_i) 
     from = requested_date - 1 
     to = requested_date + 1 
     @posts = Post.find_by_sql([ "SELECT * FROM posts WHERE created_at BETWEEN ? AND ?", from, to]) 
    elsif (year && month) 
     requested_month = Date.new(year.to_i, Date.parse(month).month.to_i) 
     end_month = requested_month.end_of_month 
     @posts = Post.find_by_sql([ "SELECT * FROM posts WHERE created_at BETWEEN ? AND ?", requested_month, end_month ]) 
    else 
     requested_year = Date.new(year.to_i) 
     @posts = Post.find_by_sql([ "SELECT * FROM posts WHERE created_at BETWEEN ? AND ?", requested_year, requested_year.end_of_year ]) 
    end 
    end 

#routes.rb 
    match ':controller/:action/:year/(:month)/(:day)', via: [:get], 
    :controller => "posts", 
    :action => "archive", 
    :year => /\d{4}/, 
    :month => /\w+/, 
    :day => /\d{2}/ 

相反,我曾用“上岗”和路径形式的博客www.site.com/posts/archive/2015/September/20