ruby on rails - Loop Trick - How to show one attribute if...? -


i'm looking function can use in loop this:

<% rink in @rinks_in_region %>       <%= rink.city #show if city (n-1) != n %>      <%= link_to_rink(rink.name+" ice rink",rink) %>     <br> <% end -%> 

basically show city if it's different previous one.

make sense? help!

alextoul

you use group_by method on @rinks_in_region group rinks city , use groupings display cities , rinks. returns hash mapping thing grouping by, city in case, values in original collection in group. so:

<% @rinks_in_region.group_by(&:city).each_pair |city, rinks| %>     <%= city %>    <% rinks.each |rink| %>     <%= link_to_rink(rink.name+" ice rink",rink) %>     <br/>   <% end -%> <% end -%> 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -