2012-03-25 151 views
0

我正在運行一個託託助力的博客,並試圖按日期正確地對帖子進行排序(如果我在一天中發帖不止一次,那麼文章按字母順序排序)。現在在我的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莫名其妙,但不知道如何。

回答

1

一個叫做時間字段添加到您的文章:

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

相關問題