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| %> <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 -- Posted via http://www.ruby-forum.com/.
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 Tony <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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| %> > <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 > -- > Posted viahttp://www.ruby-forum.com/.
On Thu, Nov 12, 2009 at 5:58 PM, Tony Tony <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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| %> > <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 >Tony, you should be able to do something like this: <% for task in tasks %> ... <% tag :hr unless task == tasks.last %> <% end %> Good luck, -Conrad> -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Thu, Nov 12, 2009 at 7:20 PM, Conrad Taylor <conradwt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Thu, Nov 12, 2009 at 5:58 PM, Tony Tony < > rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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| %> >> <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 >> > > 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> <% end %> > > Good luck, > > -Conrad > > >> -- >> >> Posted via http://www.ruby-forum.com/. >> >> >> >> >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Conrad Taylor wrote:> On Thu, Nov 12, 2009 at 7:20 PM, Conrad Taylor <conradwt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > >>> The code is this... >>> <hr/> <!-- THIS IS WHAT I NEED TO NOT DISPLAY ON TH LAST RECORD --> >>> >> <% for task in tasks %> >> >> ... >> >> <% tag :hr unless task == tasks.last %> >> > > The above should be > > <%= tag :hr unless task == tasks.last %> > > -ConradThanks 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 -- Posted via http://www.ruby-forum.com/.
2009/11/13 Tony Tony <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > Conrad Taylor wrote: >> On Thu, Nov 12, 2009 at 7:20 PM, Conrad Taylor <conradwt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >> wrote: >> >>>> The code is this... >>>> <hr/> <!-- THIS IS WHAT I NEED TO NOT DISPLAY ON TH LAST RECORD --> >>>> >>> <% 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. >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:> On Thu, Nov 12, 2009 at 5:58 PM, Tony Tony > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org >> 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, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org> > Good luck, > > -Conrad-- Posted via http://www.ruby-forum.com/.
Tony Tony 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| %> > <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! > -TonyBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.