Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby On Rails each method

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?

like image 708
user3591126 Avatar asked May 01 '26 22:05

user3591126


1 Answers

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 %>
like image 89
Marek Lipka Avatar answered May 03 '26 13:05

Marek Lipka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!