Say you have an ordered array like this generated from an address database:
[ { city: Sacramento, state: CA }, { city: San Francisco, state: CA }, { city: Seattle, state: WA } ]
And you want to generate HTML this way:
<p>CA</p> <ul> <li>Sacramento</li> <li>San Francisco</li> </ul> <p>WA</p> <ul> <li>Seattle</li> </ul>
So you are grouped by state. One way to do this is to remember the last line in each iteration of the loop and display UL status and book tags only if the current state of the line matches the state of the last lines. This seems like nasty and not Ruby-y.
Anyone have any tips on the elegant Ruby / Rails approach?
ruby ruby-on-rails
weotch
source share