Say I want to call .each on @users and in my erb I have:
<% @user.each do |user| %>
<p><%= user.name %></p>
<% end %>
Simple enough. But after every 5th user I need to add a clearfix of this:
<div class="clearfix visible-xs"></div>
What is the best way to go about this?
Enumerable#each_with_index
should be ok:
<% @users.each_with_index do |user, index| %>
<p><%= user.name %></p>
<% if (index + 1) % 5 == 0 %>
<div class="clearfix visible-xs"></div>
<% end %>
<% end %>
Maybe .each_slice
will be another way to provide this functionality:
<% @users.each_slice(5) do |users| %>
<% users.each do |user| %>
<p><%= user.name %></p>
<% end %>
<div class="clearfix visible-xs"></div>
<% end %>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.