简体   繁体   中英

Spit out a list of the last 10 days in Rails 3

I feel I should qualify all of my questions by saying I am brand new to Rails 3, so forgive me my newbiness. I've been reading the ActionView Date Helpers, but cannot figure out how to complete this simple function. In Rails 3, I want to spit out an html list with the last 10 days. So, for instance, it would look like this today:

<li>Tuesday, February 8</li>
<li>Monday, February 7</li>
<li>Sunday, February 6</li>
<li>Saturday, February 5</li>
<li>Friday, February 4</li>
<li>Thursday, February 3</li>
<li>Wednesday, February 2</li>
<li>Tuesday, February 1</li> 
<li>Monday, January 31</li>
<li>Sunday, January 30</li>

It seems like it would be simple, but I cannot figure it out. Anyone care to give it a shot?

<%- Date.today.downto(Date.today - 9.days) do |date| %>
  <li><%= date %></li>
<%- end %>

You may want to try looking into strftime to allow you to format information about dates. A website with some ruby strftime formatting can be fount at http://www.foragoodstrftime.com/

This should do it:

<% 10.downto(1).each do |day| %>
 <li><%= day.days.ago.strftime("%A, %B %d") %></li>   
<% end %>

怎么样:

<%= (0..10).map { |i| "<li>#{i.days.ago.strftime('%A, %B %d')}</li>" }.join %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM