Rails in groups of


/ Published in: Rails
Save to your folder(s)



Copy this code and paste it in your HTML
  1. %w(1 2 3 4 5 6 7).in_groups_of(3, '&nbsp;') {|group| p "<tr>" << (group.collect! {|x| "<td>#{x}</td>" }.to_s) << "</tr>" }
  2.  
  3. # returns:
  4. #"<tr><td>1</td><td>2</td><td>3</td></tr>"
  5. #"<tr><td>4</td><td>5</td><td>6</td></tr>"
  6. #"<tr><td>7</td><td>&nbsp;</td><td>&nbsp;</td></tr>"
  7.  
  8. # The better rails way:
  9. <table>
  10. <% %w(1 2 3 4 5 6 7).in_groups_of(3, '&nbsp;') do |group| %>
  11. <%= content_tag(:tr, (group.collect! {|x| content_tag(:td, "#{x} hoo!") } ) ) %>
  12. <% end %>
  13. </table>
  14. # returns:
  15. # <table>
  16. # <tbody>
  17. # <tr><td>1 hoo!</td><td>2 hoo!</td><td>3 hoo!</td></tr>
  18. # <tr><td>4 hoo!</td><td>5 hoo!</td><td>6 hoo!</td></tr>
  19. # <tr><td>7 hoo!</td><td>&nbsp; hoo!</td><td>&nbsp; hoo!</td></tr>
  20. # </tbody>
  21. # </table>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.