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| %>   <h2><%= month.strftime('%B') %></h2>   <% for task in tasks %>     <div class="task">       <strong><%= task.name %></strong>       due on <%= task.due_at.to_date.to_s(:long) %>     </div>   <% end %>

   <hr/> <!-- THIS IS WHAT I NEED TO NOT DISPLAY ON TH LAST RECORD -->

<% 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 %>   <%= '<hr/>' 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

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 %>

<div class="task">

  <strong><%= [task.name](http://task.name) %></strong>

  due on <%= task.due_at.to_date.to_s(:long) %>

</div>

<% 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

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

<% for task in tasks %>

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

<% end %>

Good luck,

-Conrad

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 %>

<div class="task">

  <strong><%= [task.name](http://task.name) %></strong>

  due on <%= task.due_at.to_date.to_s(:long) %>

</div>

<% 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

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

<% for task in tasks %>

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

The above should be

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

-Conrad

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

Conrad Taylor wrote:

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

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,

Tony Tony wrote:

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| %>   <h2><%= month.strftime('%B') %></h2>   <% for task in tasks %>     <div class="task">       <strong><%= task.name %></strong>       due on <%= task.due_at.to_date.to_s(:long) %>     </div>   <% end %>

   <hr/> <!-- THIS IS WHAT I NEED TO NOT DISPLAY ON TH LAST RECORD -->

<% end %>

In addition to my other suggestion, you might consider using a <table> element instead of all the <hr>s. Since this is a table of data, the semantics are more appropriate to the <table> 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 %>   <%= '<hr/>' unless task == tasks.last %>   ...

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

Many thanks! -Tony

Best,