Hello,
I apologize if this has been asked before.
I am interested in creating a helper for my view that generates a
grid-style table. Here''s what I have in application_helper.rb:
def print_grid_layout(items, table_class, num_columns)
print %Q{<table class=''#{table_class}''
cellpadding=''0'' cellspacing=''0''>\n}
rows = Array.new
# creates an array of arrays containing num_column items
num_columns.times do
rows << items.slice!(0..num_columns-1)
end
# for each row of items
rows.size.times do |i|
# figure out if we''re printing the top row, or subsequent rows.
if i.eql?(0) then row_class = "top" else row_class =
"" end
print %Q{\s\s<tr class="#{row_class}">\n}
rows[i].each do |item|
print yield(item)
end
(num_columns - rows[i].size).times
print %Q{\s\s\s\s<td
class="blank"> </td>\n}
end
print %Q{\s\s</tr>\n}
end
The problem I am having is with the yield statement. In the template I
want my block to be the html for the cell, like so:
<% print_grid_layout(@product.product_variations, "variations", 3)
do
|product_variation| %>
<td class="normal" id="ProductVariation_<%=
product_variation.id %>">
<strong><%=h product_variation.title %></strong>
<br />
Catalog Num: <%=h product_variation.catalog_num %> <br />
Price: $<%=h product_variation.price %> <br />
</td>
<% end %>
It doesn''t seem to like the rhtml as a block.
Am I approaching the problem wrong? If so, what should I be doing instead?