如何按日期和时间对帖子进行排序?

问题描述:

我正在运行一个托托助力的博客,并试图按日期正确地对帖子进行排序(如果我在一天中发帖不止一次,那么文章按字母顺序排序)。现在在我的config.ru我对日期# set :date, lambda {|now| now.strftime("%d/%m/%Y") }和基本设置时间# set :time, lambda {|now| now.strftime("at %H:%I%p") } 如何按日期和时间对帖子进行排序?

在我layout.rhtml文章设置的排序,像这样:<% articles.select {|a| a[:date] <= Date.today}[0..4].each do |article| %>我知道我需要在里面添加:time莫名其妙,但不知道如何。

一个叫做时间字段添加到您的文章:

require 'time' 

class Article 
    def timestamp 
    self[:timestamp] ||= Time.parse("#{self[:date].strftime("%Y-%m-%d")} #{self[:time]}") 
    end 
end 

toto = Toto::Server.new do 

现在,在你的布局可以使用timestamp方法排序:

title: The Wonderful Wizard of Oz 
author: Lyman Frank Baum 
date: 1900/05/17 
time: 12:30:00 PST 

Dorothy lived in the midst of the great Kansas prairies, with Uncle Henry, 
who was a farmer, and Aunt Em, who was the farmer's wife. 

猴服务器块之前修补Article类:

<% articles.select {|a| a.timestamp <= Time.now}[0..4].each do |article| %> 
+0

谢谢,这没有把戏。 – user1290707 2012-03-25 03:16:00