Getting the last record for an each loop?

Hi all,

I need a very similar functionality to what is displayed on the group_by
Month railscast (#29 group_by Month - RailsCasts).

The code is this…

<% @task_months.sort.each do |month, tasks| %>

<%= month.strftime('%B') %>

<% for task in tasks %>
<%= task.name %> due on <%= task.due_at.to_date.to_s(:long) %>
<% end %>

<% end %>

The HR tag above is what I need to not show in the page. I know I could
this easily in a for loop with something like:

<% for task in tasks %>
<%= ‘


’ unless task == tasks.last %>

Anyone have any clue how to do this with an each loop?

Many thanks!
-Tony

tasks.each_with_index do |task, index|

<%= content_tag(:hr) unless index == tasks.size %>

might need a +1 on either of index or .size, but that’s the idea.

-eric

On Nov 12, 5:58 pm, Tony T. [email protected]

On Thu, Nov 12, 2009 at 5:58 PM, Tony T.
<[email protected]

wrote:

<%= month.strftime('%B') %>

Many thanks!
-Tony

Tony, you should be able to do something like this:

<% for task in tasks %>

<% tag :hr unless task == tasks.last %>
<% end %>

Good luck,

-Conrad

On Thu, Nov 12, 2009 at 7:20 PM, Conrad T. [email protected]
wrote:

The code is this…


<% for task in tasks %>

<% tag :hr unless task == tasks.last %>

The above should be

<%= tag :hr unless task == tasks.last %>

-Conrad

Conrad T. wrote:

On Thu, Nov 12, 2009 at 7:20 PM, Conrad T. [email protected]
wrote:

The code is this…


<% for task in tasks %>

<% tag :hr unless task == tasks.last %>

The above should be

<%= tag :hr unless task == tasks.last %>

-Conrad

Thanks for the reply guys… I’ve tried doing all of the suggestions but
it still doesn’t work. I suspect it may be something to do with the sort
call?

<% @task_months.sort.each do |month, tasks| %>

By the way - The issue is with the each loop, not the for loop.

Thanks again!
-Tony

Conrad T. wrote:

On Thu, Nov 12, 2009 at 5:58 PM, Tony T.
<[email protected]

wrote:

<%= month.strftime('%B') %>

Many thanks!
-Tony

Tony, you should be able to do something like this:

<% for task in tasks %>

<% tag :hr unless task == tasks.last %>
<% end %>

Better:

<% tasks.collect do |task| %>
HTML markup
<% end.join tag(:hr) %>

(some % may need to be %=)

Better still: refactor the whole thing into partials and/or helpers.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Good luck,

-Conrad

2009/11/13 Tony T. [email protected]:

  …

Thanks for the reply guys… I’ve tried doing all of the suggestions but
it still doesn’t work. I suspect it may be something to do with the sort
call?

<% @task_months.sort.each do |month, tasks| %>

By the way - The issue is with the each loop, not the for loop.

Have a look at the rails guide on Debugging and then break in to the
loop and work out what is going on. (use ruby-debug). You can inspect
each variable and see what is not as you expect.

Colin

Tony T. wrote:

Hi all,

I need a very similar functionality to what is displayed on the group_by
Month railscast (http://railscasts.com/episodes/29-group-by-month).

The code is this…

<% @task_months.sort.each do |month, tasks| %>

<%= month.strftime('%B') %>

<% for task in tasks %>
<%= task.name %> due on <%= task.due_at.to_date.to_s(:long) %>
<% end %>

<% end %>

In addition to my other suggestion, you might consider using a


element instead of all the
s. Since this is a table of data, the
semantics are more appropriate to the
element.

The HR tag above is what I need to not show in the page. I know I could
this easily in a for loop with something like:

<% for task in tasks %>
<%= ‘


’ unless task == tasks.last %>

Anyone have any clue how to do this with an each loop?

Many thanks!
-Tony

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]