Note that there are some explanatory texts on larger screens.

plurals
  1. POIn a Rails view, how iterate a group of objects that should display as rows of 4 for instance?
    text
    copied!<p>Right now I am using the modulus operator and doing something like this: The purpose is just to close the previous and create a new "row-fluid" div after every fourth item in the array/result</p> <pre><code>&lt;div class="row-fluid"&gt; &lt;% Group.all.each_with_index do |g, index| %&gt; &lt;% if index != 0 &amp;&amp; index % 4 == 0 %&gt; &lt;/div&gt; &lt;br&gt; &lt;div class="row-fluid"&gt; &lt;% end %&gt; &lt;div class="col-lg-3" id="my_group_tile_&lt;%= g.id %&gt;"&gt; &lt;div class="tiletop"&gt; &lt;div class="span12"&gt; &lt;%= cl_image_tag(g.avatar, :crop =&gt; :pad, :height =&gt; 200, :width =&gt; 250, :class =&gt; "tile-image") %&gt; &lt;/div&gt; &lt;h4 id="tileheader"&gt; &lt;a href="/groups/&lt;%= g.id %&gt;"&gt;&lt;%= g.title %&gt; &amp;nbsp; &lt;/a&gt; &lt;/h4&gt; &lt;/div&gt; &lt;div class="tile-info"&gt; &lt;%= truncate(g.info, :length =&gt; 100, :omission =&gt; '... (continued)') %&gt; &lt;/div&gt; &lt;hr&gt; &lt;span id="group_members_count_&lt;%= g.id %&gt;"&gt; &lt;span class="badge badge-black" style="color:white"&gt; &lt;%= g.members.count %&gt; members &lt;/span&gt; &lt;/span&gt; &lt;span id="join_&lt;%= g.id %&gt;" class="btn-wrapper"&gt; &lt;%= render :partial =&gt; 'join_button', :locals =&gt; { :group =&gt; g } %&gt; &lt;/span&gt; &lt;/div&gt; &lt;% end %&gt; &lt;/div&gt; </code></pre> <p><strong>UPDATE</strong>: after using the accepted answer things are quite a bit cleaner.</p> <pre><code>&lt;% Group.all.each_slice(4) do |slice_of_four| %&gt; &lt;div class="row"&gt; &lt;div class="bs-example"&gt; &lt;div class="row"&gt; &lt;% slice_of_four.each do |g| %&gt; &lt;div class="col-lg-3" id="my_group_tile_&lt;%= g.id %&gt;"&gt; &lt;a href="#" class="thumbnail"&gt; &lt;%= cl_image_tag(g.avatar, :crop =&gt; :pad, :height =&gt; 180, :width =&gt; 266, :class =&gt; "tile-image") %&gt; &lt;/a&gt; &lt;/div&gt; &lt;% end %&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload