[英]Ruby On Rails each method
Say I want to call .each on @users and in my erb I have: 假设我想在@users和我的erb上打电话给.each我有:
<% @user.each do |user| %>
<p><%= user.name %></p>
<% end %>
Simple enough. 很简单。 But after every 5th user I need to add a clearfix of this: 但在每第5个用户之后,我需要添加一个clearfix:
<div class="clearfix visible-xs"></div>
What is the best way to go about this? 最好的方法是什么?
Enumerable#each_with_index
should be ok: Enumerable#each_with_index
应该没问题:
<% @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: 也许.each_slice
将是另一种提供此功能的方法:
<% @users.each_slice(5) do |users| %>
<% users.each do |user| %>
<p><%= user.name %></p>
<% end %>
<div class="clearfix visible-xs"></div>
<% end %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.