6

I'm trying to display a objects in my model in the form of a table, but am having a hard time trying to create the rows and columns dynamically.

I have a model called Pictures and currently I'm displaying all of them in a looooong list.

<% for picture in @pictures %>
   <p><%= image_tag(picture.url) %></p>
<% end %>

How can I turn this into a table in the view with rails?

<table>
<tr>
<% for picture in @pictures %>   
     <td> 
        <%= image_tag(picture.url) %>
     </td>
** Here's where I'm confused..How can I write after 6 cells create a new row?? *****
<% end %>
</table>

So the question is really related to how to breakup this type of data within the view.

5 Answers 5

11

Have a look at the enumerable method ".each_slice".

http://www.ruby-doc.org/core/classes/Enumerable.html#M001514

With it you can do something along the lines of:

<table>
  <% @pictures.each_slice(6) do |slice| %>   
    <tr>
      <% slice.each do |picture| %>
        <td> 
          <%= image_tag(picture.url) %>
        </td>
      <% end %>
    </tr>
  <% end %>
</table>

You may have to do some fiddling to populate the last row nicely, but that should get you on your way.

Sign up to request clarification or add additional context in comments.

1 Comment

I knew ruby/rails had to have something along these lines. I've not had to do this, though, so never done the research.
1

I really dig the each_slice solution. Just thought I'd chime in if you wanted to have a non-tabular view, you could find out your max-width and max-height for your images and set a container around your photos and just use css to float them all together. Each row would contain however many would fit in your containing div.

view

<% for picture in @pictures %>
   <div class="photo">
     <%= image_tag(picture.url) %>
   </div>
   <div class="float_clear"></div>
<% end %>

css

.photo {height: 150px; width: 150px; float:left;margin:0 10px; 10px 0;}
.photo img{max-height: 150px; max-width: 150px;}
.float_clear{clear:both;}

2 Comments

I agree, this doesn't answer the question asked. But probably provides the solution that would be most correct in terms of html and responsive layout. Tables should not be used for layout if there are other options
Thanks @jonas! I'm sure there are a bunch of different ways to do this while following best practices as web views are more demanding across mobile devices.
0

I'm sure ruby on rails has some helpers for this - but if not, here's how I've approached this kind of layout in my past life as a low level php developer:

Keep a count, modulo 6 (i.e. i = (i + 1) % 6). If i == 0, output <tr></tr>.

So, something like:

<% i = 0 %>
<tr>
  <%
    @pictures.each do |picture|
      i = (i + 1) % 6
  %>
    <%= '<tr></tr>' if i == 0 %>
    <td>
      <%= image_tag(picture.url) %>
    </td>
  <% end %>
</tr>

1 Comment

If you use this approach, instead of setting "i", you can use the each_with_index method.
0

I would use % (modulus), something like this:

<table>
  <% rows = 0 %>
  <% @pictures.each do |picture| %>
    <%= "<tr>" if rows % 6 %>
    <td><%= image_tag(picture.url) %></td>
    <% rows += 1 %>
    <%= "</tr>" if rows % 6 %>
  <% end %>
  <%= "</tr>" unless rows % 6 %>
</table>

Comments

0

Another nice way of doing this in rails is using the method in_groups_of.

So you could have

<table>
  <% @pictures.in_groups_of(6, false) do |group| %>   
    <tr>
      <% group.each do |picture| %>
        <td> 
          <%= image_tag(picture.url) %>
        </td>
      <% end %>
    </tr>
  <% end %>
</table>

Here's the documentation for that method: http://apidock.com/rails/Array/in_groups_of

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.