1

I'm building a small app where a user can make entries into a text box, press enter and then the string they entered will be recorded in a section below the text box.

The things the person enters, called Lessons are displayed in the bottom of the page with the following code.

<% @allLessons.each do |lesson| %> 
    <tr>
        <td class="my-lessons">
            <b><%= lesson.content %></b> – <%= lesson.created_at.strftime("%b %d, %Y") %>
            <br />
        </td>
    </tr>
    <% end %>

I'm trying to write something that looks at the date of each lesson and if that date is greater than the previous day's date, it inserts a line break between the outputted entries.

I'm very puzzled on how to do this. Would it be best to do all embedded Ruby in home.html.erb or use the pages controller to help out here as well?

4

2 回答 2

0

为了扩展前面的答案,假设和都是正确的 Time 对象,类似的东西lesson.created_at.strftime("%Y%m%d").to_i > previous_lesson_date.strftime("%Y%m%d).to_i可以解决上述问题。lesson.created_atprevious_lesson_date

可能有一个更直接的解决方案可以更好地利用 Time 对象。

于 2012-05-24T14:30:30.970 回答
-1

就像是:

<% @allLessons.each do |lesson| %>
  <% previous_lesson_date = lesson.created_at %>
    <tr>
        <td class="my-lessons">
            <b><%= lesson.content %></b> – <%= lesson.created_at.strftime("%b %d, %Y") %>
            <% if lesson.created_at > previous_lesson_date %>
              <br />
            <% end %>
        </td>
    </tr>
<% end %>

是字面上的答案。由于当前课程几乎总是有一个更大的日期(因为它将在几秒钟后创建)。我认为你可能需要使用 Rails 日期函数来获得你真正想要的东西。

于 2012-05-24T14:13:17.910 回答