Return to Snippet

Revision: 24773
at March 10, 2010 19:09 by inkdeep


Initial Code
%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>" }

# returns:
#"<tr><td>1</td><td>2</td><td>3</td></tr>"
#"<tr><td>4</td><td>5</td><td>6</td></tr>"
#"<tr><td>7</td><td>&nbsp;</td><td>&nbsp;</td></tr>"

# The better rails way:
 <table>
   <% %w(1 2 3 4 5 6 7).in_groups_of(3, '&nbsp;') do |group| %>
     <%= content_tag(:tr, (group.collect! {|x| content_tag(:td, "#{x} hoo!") } ) )  %>
   <% end %>
 </table>
# returns:
# <table>
#   <tbody>
#     <tr><td>1 hoo!</td><td>2 hoo!</td><td>3 hoo!</td></tr>
#     <tr><td>4 hoo!</td><td>5 hoo!</td><td>6 hoo!</td></tr>
#     <tr><td>7 hoo!</td><td>&nbsp; hoo!</td><td>&nbsp; hoo!</td></tr>
#   </tbody>
# </table>

Initial URL


Initial Description


Initial Title
Rails in groups of

Initial Tags
rails, ruby

Initial Language
Rails