Rails视图中的嵌套循环

时间:2011-10-31 15:26:49

标签: ruby-on-rails

我在视图中嵌套了网格,并希望为每个网格使用相同的CSS类。当我使用两个嵌套的cycle( "odd", "even" )助手时,我得到三个混乱的CSS,如odd

示例网格

<% first_collection.each do |entry| %>
  <tr class="<%= cycle( "odd", "even" ) %>">
    # some info here
    <% entry.records.each do |record| %>
      <div class="<%= cycle( "odd", "even" ) %>">
        <%= record.value %>
      </div>
    <% end %> 
  </tr>
<% end %>

所以问题是 - 是否可能有两个相同的嵌套循环,彼此之间没有混乱?

1 个答案:

答案 0 :(得分:8)

您可以为每个循环调用应用一个名称,以使它们分开。

<% first_collection.each do |entry| %>
  <tr class="<%= cycle( "odd", "even", :name => 'first_collection ) %>">
    # some info here
    <% entry.records.each do |record| %>
      <div class="<%= cycle( "odd", "even", :name => 'entries' ) %>">
        <%= record.value %>
      </div>
    <% end %> 
  </tr>
<% end %>