views:

24

answers:

2

I have a store application and want my store items to display in a 3 column catalog. I want the 4th item to move to a new row automatically after the 3rd item is placed on the 3rd column on the first row. All items will be called from the database.

How can I acheive displaying my product catalog like thumbnails. At the moment this is what I have in my index.html.erb

<% @products.each do |product| %>
<table>
  <tr>
    <th>
<div class="entry">
  <div class="card-title" >
<%=h product.title %></div> <%= image_tag(product.image_url) %>

  <div class="price-line">
    <span class="price"><%= number_to_currency(product.price, :unit => "N", :format => "%u%n") %></span>
  </div>

  <% form_remote_tag :url => { :action => 'add_to_cart', :id => product } do %>
    <%= submit_tag "Add to Cart" , :class => "add_button" %>
    <% end %>

</div>

    </th>
</tr>
</table>
<% end %>

At the moment every item is shown in just one column but I want to display items like this:

Item A   Item B  Item C
Item D   Item E  Item F
Item G   ......  ......

Any help will be appreciated. I'm new to RoR.

Thanks. Ijespal

A: 

I'm not sure if this is the best way, but you can use something like

<table>
   <% @products.each_with_index do |product, index| %>

      <% if index%3==0 %>
         <tr>
      <% end %>

      <td><%= product.name%></td>

      <% if (index+1)%3==0 %>
         </tr>
      <% end %>

    <% end %>
</table>

Worked just fine here (:

Edit

It works, but edgerunner's answer is much better! :]

j.
Thank J. I'll try out both methods to see which works best. Really appreciate the assistance.
J, your method work pretty fine. I get an error with edgerunner's method. Thanks loads.
A: 

Rails has a nice helper for that: Array#in_groups_of

<table>
  <% @products.in_groups_of(3, false) do |row| %>
    <tr>
      <% row.each do |product| %>
        <td><%= product.name # and whatever else you want %></td>
      <% end %>
    </tr>
  <% end %>
</table>

The in_groups_of helper normally returns an array of arrays, which all have the size set by the first parameter. If the original number of elements are unable to fill the last array completely, then it is padded by nil values. If you pass false as a second argument, then the last array is left shorter than the others, without nil elements.

edgerunner
Much better than mine heheh Didn't know about this method. Nice! :]
j.
Thanks edgerunner. Will try it out this method and revert. Thanks loads.
edgerunner when I tried this method I get an error undefined method `title' for nil:NilClass . From this line <%= product.title %></div> <%= image_tag(product.image_url) %>. What might be wrong? But J's method works fine.
The `in_groups_of` helper normally returns an array of arrays, which all have the size set by the first parameter. If the original number of elements are unable to fill the last array completely, then it is padded by `nil` values, and that's why you get the error. If you pass `false` as a second argument, then the last array is left shorter than the others, without nil elements. That way it should work for you. I've updated the answer to add the `false` parameter to my `in_groups_of` call.
edgerunner
Great! It works now with false passed as a parameter. Thank you edgerunner.
You're welcome.
edgerunner