Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 例如,在Rails视图中,如何迭代一组应该显示为4行的对象?_Ruby_Ruby On Rails 3 - Fatal编程技术网

Ruby 例如,在Rails视图中,如何迭代一组应该显示为4行的对象?

Ruby 例如,在Rails视图中,如何迭代一组应该显示为4行的对象?,ruby,ruby-on-rails-3,Ruby,Ruby On Rails 3,现在我正在使用模运算符并做如下操作: 其目的只是在数组/结果中每四个项目之后关闭上一个并创建一个新的“row fluid”div <div class="row-fluid"> <% Group.all.each_with_index do |g, index| %> <% if index != 0 && index % 4 == 0 %> </div> <br> <div class="row-fl

现在我正在使用模运算符并做如下操作: 其目的只是在数组/结果中每四个项目之后关闭上一个并创建一个新的“row fluid”div

<div class="row-fluid">
  <% Group.all.each_with_index do |g, index| %>
    <% if index != 0 && index % 4 == 0 %> </div> <br> <div class="row-fluid"> <% end %>
    <div class="col-lg-3" id="my_group_tile_<%= g.id %>">
      <div class="tiletop">
        <div class="span12">
         <%= cl_image_tag(g.avatar, :crop => :pad, :height => 200, :width => 250, :class => "tile-image") %>
         </div>
        <h4 id="tileheader"> 
          <a href="/groups/<%= g.id %>"><%= g.title %> &nbsp; </a>  
        </h4>
      </div>
    <div class="tile-info">
      <%= truncate(g.info, :length => 100, :omission => '... (continued)') %>
    </div>    
    <hr>
    <span id="group_members_count_<%= g.id %>">    
      <span class="badge badge-black" style="color:white">
        <%= g.members.count %> members
      </span> 
    </span>
    <span id="join_<%= g.id %>" class="btn-wrapper">
      <%= render :partial => 'join_button', :locals => { :group => g } %>
    </span>
  </div>
  <% end %>
</div>


:pad,:height=>200,:width=>250,:class=>“平铺图像”)%%> 100,:省略=>'。。。(续)%>
成员 '加入按钮',:locals=>{:group=>g}%>
更新:使用接受的答案后,事情变得更干净了

<% Group.all.each_slice(4) do |slice_of_four| %>
  <div class="row">
    <div class="bs-example">
      <div class="row">
        <% slice_of_four.each do |g| %>
          <div class="col-lg-3" id="my_group_tile_<%= g.id %>">
            <a href="#" class="thumbnail">
              <%= cl_image_tag(g.avatar, :crop => :pad, :height => 180, :width => 266, :class => "tile-image") %>
            </a>
          </div>
        <% end %>
      </div>
    </div>
  </div>  
<% end %>


谢谢

您可以尝试使用每个\u片枚举器


尝试不使用Group.all,它会将所有组存储在内存中,如果groups表很大,则会出现不必要的问题,这看起来不错。对于如何在加载组进行分页时提高效率,您有什么建议吗?实际上,您有很多选择,如果您使用,这将为您解决这两个问题(内存+切片),或者如果没有gem,您可以使用.limit(4)一次加载4条记录,但这两个选项都会过多。相反,我会建议使用find_each(尝试他们拥有的不同选项),我实现了你的each_slice建议,而且它更干净。在这一点上,我没有处理大量的组,但是批处理文档确实看起来很有趣。谢谢
Group.all.each_slice(4) do |slice_of_four|
   *now you can do your operations on the slices of four*
end