If you don't like iterating over the instances a dozen times, you could make an array of the headers, iterate over the headers, then iterate over instances once within that block.
James On Monday, September 17, 2012 at 11:42 AM, Chris McCann wrote: > Scott, > > Thanks for the speedy reply! > > The array approach was what I originally came up with but it felt a bit raw. > Seems like there should some sort of <%= content_for ... %> approach or > similar that would be more elegant. > > It's not really an issue of performance since all the data needed will be in > memory when the collection is initially loaded. It's about trying not to > create this gawd-awful ugly view with a dozen <% @instances.each do > |instance| %> calls to loop through the model objects each time I want to > create a row of data. > > It just "feels" wrong and I'm hoping someone has tackled this before and come > up with a clever solution. > > Cheers, > > Chris > > On Mon, Sep 17, 2012 at 11:37 AM, Scott Olmsted <[email protected] > (mailto:[email protected])> wrote: > > Chris, > > > > First question: is the performance impact enough that it's worth bothering > > about? Premature optimization, and all that. > > > > Second, how about initializing an array for each row, looping through the > > models once, tacking the fields you want on to the arrays, and then pulling > > from the arrays to construct the view? > > > > Scott > > > > > > On Monday, September 17, 2012 11:15:40 AM UTC-7, Chris McCann wrote: > > > SD Ruby, > > > > > > I'm wrestling with a view that displays data from a collection of model > > > objects in an HTML table with the data from each model instance shown in > > > a column. I sense there's an elegant way to do what I'm trying to do but > > > I'm at a loss as to how. > > > > > > The markup below shows how the end result should look. What I'm trying > > > to avoid is having to look through the collection of instances to build > > > each row. > > > > > > <div class="summary"> > > > <table> > > > <tr> > > > <th class="first"><a href="#">Summary</a></th> > > > <!-- loop through collection and write this data for each instance --> > > > <th><a href="#">201 First St.</a></th> > > > <th><a href="#">1133 Columbia St.</a></th> > > > </tr> > > > <tr> > > > <td class="first">Start Date</td> > > > <!-- loop through collection and write this data for each instance --> > > > <td>01/01/2010</td> > > > <td>01/01/2010</td> > > > </tr> > > > <tr> > > > <td class="first">Term</td> > > > <!-- loop through collection and write this data for each instance --> > > > <td>84 Months</td> > > > <td>84 Months</td> > > > </tr> > > > </table> > > > </div> > > > > > > > > > Is there a better, more Railsy approach that I'm just not seeing? The > > > actual table is much more complex than what's shown here so I don't want > > > to go the CSS-only no table route unless there's a really nice solution > > > that entails not using tables. > > > > > > Cheers, > > > > > > Chris > > -- > > SD Ruby mailing list > > [email protected] (mailto:[email protected]) > > http://groups.google.com/group/sdruby > -- > SD Ruby mailing list > [email protected] (mailto:[email protected]) > http://groups.google.com/group/sdruby -- SD Ruby mailing list [email protected] http://groups.google.com/group/sdruby
