2012-03-25 3 views
0

전 toto powered 블로그를 운영하고 있습니다. 날짜별로 게시물을 정확하게 정렬하려고합니다. 하루에 두 번 이상 게시물을 올리면 해당 날짜에 사전 순으로 기사가 정렬됩니다. 지금 내 config.ru에 나는과 같이 분류되어 # set :date, lambda {|now| now.strftime("%d/%m/%Y") }와 날짜에 대한 기본 설정 및 내 layout.rhtml 기사에서게시물을 날짜와 시간순으로 정렬하려면 어떻게해야합니까?

시간 # set :time, lambda {|now| now.strftime("at %H:%I%p") } 에 대한 설정이 있습니다 <% 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. 

원숭이가 서버 블록 전에 제 클래스를 패치 :

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

감사합니다. – user1290707

관련 문제