ruby-on-rails - 每 3 个项目 haml new tr

标签 ruby-on-rails haml

我有以下代码(简化为切入点):

    - @assumptions[1].each do |ca|
      - if count % 3 == 1
        %tr
          %th
            = ca.value
      - else
        %th
          = ca.value
      - count = count + 1

我不确定如何在 haml 中进行这项工作,让每 4 个项目创建一个新标签。

这是它的输出方式:
  <tr>
    <tr> 
      <th> 
        700.0
      </th> 
    </tr> 
    <th> 
      1235.0
    </th> 
    <th> 
      0.8
    </th> 
    <th> 
      650.0
    </th> 
  <tr> 
    <th> 
      1050.0
    </th> 
  </tr> 
  <th> 
    0.2
  </th> 
</tr>

这是我希望它输出的方式:
<tr>
  <th>
    700.0
  </th>
  <th>
    1235.0
  </th>
  <th>
    0.8
  </th>
</tr>
<tr>
  <th>
    650.0
  </th>
  <th>
    1050.0
  </th>
</tr>

我希望这是有道理的。

最佳答案

您可以使用 Enumerable's each_slice method将这些分组为 3 个块:

- @assumptions[1].each_slice(3) do |group|
  %tr
    - group.each do |ca|
      %th= ca.value

关于ruby-on-rails - 每 3 个项目 haml new tr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6347028/

相关文章:

javascript - 如何使用 jQuery 查找 DIV 的渲染高度?

javascript - haml/javascript/erb 转义

ruby-on-rails - 使用 Pow 作为服务器在 RubyMine 中调试 - Ruby 2.1.1 + Rails 4

ruby-on-rails - rails : Using CanCan to assign multiple roles to Users for each organization they belong to?

javascript - 通过 ajax 将 javascript 变量(字符串)传递给 Controller ​​ - 404

javascript - 在 rails 3.2.2 中使用 $.ajax() 异步获取数据

ruby-on-rails - 在Rails 3中进行就地编辑

ruby-on-rails - rails : NoMethodError in production only

ruby-on-rails - 火腿 + mustache

ruby - 即使我不懂 Ruby 也可以学习 Haml 和 Sass 吗?