如何按照列表的正确顺序显示Date.current
的“TESTING”一词?
<% @lifetime_years.sort.each do |year, lifetimes| %>
<%= year.strftime('%Y') %>
<% lifetimes.group_by { |t| t.deadline.beginning_of_month }.sort.each do |month, lifetimes| %>
<%= month.strftime('%B') %>
<% for lifetime in lifetimes %>
<%= lifetime.deadline %>: <%= lifetime.name %>
<% end %>
<% end %>
<% end %>
例如,
2016
January
18: Run a Marathon (aka lifetime object)
25: Eat a piece of poop (aka lifetime object)
February
1: Resolve SO Question (aka lifetime object)
1: TESTING
March
5: Write a Book (aka lifetime object)
换句话说,如何定位排序中的特定日期,以便我可以添加与lifetime
无关的其他信息?
<% @lifetime_years.sort.each do |year, lifetimes| %>
<%= year.strftime('%Y') %>
<% lifetimes.group_by { |t| t.deadline.beginning_of_month }.sort.each do |month, lifetime| %>
<%= month.strftime('%B') %>
<% for lifetime in lifetimes %>
<%= lifetime.deadline %>: <%= lifetime.name %>
<% end %>
#CHANGED THIS PARAGRAPH
<% @routines.each do |routine| %>
<% if lifetime.deadline == Date.current %>
<p><%= routine.action %></p>
<% else %>
NONE
<% end %>
<% end %>
<% end %>
<% end %>
这会正确列出当天的routines
,但前提是lifetime
存在deadline
。我们是否可以列出当天的所有routines
,即使不存在lifetime
?
答案 0 :(得分:1)
我认为你不需要。只需在您的ERB中注入一个if
块:
<% @lifetime_years.sort.each do |year, lifetimes| %>
<%= year.strftime('%Y') %>
<% lifetimes.group_by { |t| t.deadline.beginning_of_month }.sort.each do |month, lifetime| %>
<%= month.strftime('%B') %>
<% for lifetime in lifetimes %>
<%= lifetime.deadline %>: <%= lifetime.name %>
<% end %>
<% if lifetime == Date.current %>
<%= "Some extra information from Ruby code" %>
<p>Some extra plaintext</p>
<% else %>
<%= [lifetime, Date.current] %>
<% end %>
<% end %>
<% end %>