2
我想在rails 4中使用distance_of_time_in_words
。我在db中有一個叫做expires_at
的日期時間字段。我嘗試了幾種不同的方式來設置它的格式,但似乎無法使其正常工作。distance_of_time_in_words rails 4
<%= offer.expires_at.strftime('%I:%M %p %b %d') %> // works properly: 12:00 AM Mar 29
<%= distance_of_time_in_words(offer.expires_at) %> // fails: undefined method `to_datetime' for 0:Fixnum
<%= distance_of_time_in_words(Time.parse(offer.expires_at.to_s)) %> // fails as above
<%= distance_of_time_in_words(offer.expires_at.to_time.to_i) %> // works but shows '44 years' as distance
RoR noob的任何指針?
完美!謝謝,我假設第二個參數默認爲'Time.now'。文檔對我不清楚。 – user101289
還有另外一種方法,我將它添加到答案中,默認爲'Time.now'。 –