开发者

Rails: load table cells two across at a time

开发者 https://www.devze.com 2023-03-26 00:09 出处:网络
This seems like it should be easy, but I can\'t figure out a way to do it. Essentially I want to load (in ERB, from a collection fetched via Rails) an HTML table with cells such that the first row has

This seems like it should be easy, but I can't figure out a way to do it. Essentially I want to load (in ERB, from a collection fetched via Rails) an HTML table with cells such that the first row has the first two records, the second row has the next two, e开发者_StackOverflowtc. Something like this:

-----------
| 1  |  2 |
| 3  |  4 |
| 5  |  6 |
-----------

Seems like there would be a Ruby/Rails way to iterate over a collection two records at a time.


Ah, figured it out moments after posting, with help from this question.

For posterity's sake, here's my solution:

  <% @users.each_slice(2) do |two| %>
     <tr>
        <% two.each do |p| %>
           <td>
              <%= p.id %>
           </td>
        <% end %>
     </tr>
  <% end %>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号