views:

102

answers:

6

i have code like :

<% if @photos.blank? %>
  No Pic
<% else %>    
<center>  
    <table>
      <tr>
    <% @photos.each do |c|  %>
      <td>
<%= image_tag(url_for({:action => 'image', :id => c.id}),:width =>'20%', :height =>'20%' ) %><br>
    <%= link_to "lihat" , {:action => 'show2', :id => c.id} -%>
        <% if logged_in? %>
        <small> <%= link_to 'Edit', {:action => "edit2", :id => c.id } %></small>
        <small> <%= link_to 'Delete', {:action => "delete2", :id => c.id }, :confirm => "apakah anda yakin mau mengapus berita ini?" %></small>
        <% end %>
        </td>

        <% end %>
            </tr>

    </table>
 </center>

 <% end %>

i want to print my images "if column = 5 " automaticly add
or 5 image per row output: [image][image][image][image][image]

what should i do with my code?? please help me

+2  A: 

There are a couple of ways you could solve the "5 photos on a row" problem, but I think that this is the most expressive: group the array of photos into rows of 5 and use nested .each loops:

rows = @photos.in_groups_of(5)
rows.each do |row|
  <tr>
  row.each do |photo|
    <td>
    #display photo here
    </td>
  end
  </tr>
end

Be aware that if you don't have 5 photos on your final row, array#in_groups_of will pad it with nil unless you specify an alternative default.

Rick Grundy
way better answer than I was thinking (using each_with_index and mod on 5). Very clean!
mixonic
still error!!! how i should type if i use each_with_index for example?
Kuya
Can you be more specific about the error? It it's not directly related to Array#in_groups_of then Array#each_with_index (the second of the 'couple of ways' I mentioned ;) ) may not help you!
Rick Grundy
+1  A: 

you can do something like this

@photos.each_slice(5) do |slice|
   slice.each do |photo|
     # render photo.name, photo.title...etc here
   end
end
ez
A: 
if i modified with slice :

<h1>All Photos</h1>

<% @photos.each_slice(5) do |slice|  -%>
    <%  slice.each do %>

<h2><%= link_to photo.title,
photo_path(:user_id => photo.user, :id => photo) %></h2>
<p>

     <%= image_tag(url_for({:action => 'image', :id => photo.id}),:width => '10%',:height => '10%' ) %>
<br>
<% if is_logged_in? and logged_in_user.id == @user.id -%>


<%= link_to 'Destroy', photo_path(:user_id => photo.user, :id => photo),
:confirm => 'Are you sure?', :method => :delete %>
<% end %>

<% end -%>
</p>
<% end -%>


error :

Showing photos/index.html.erb where line #5 raised:

undefined method `title' for #

Extracted source (around line #5):

2: 
3: <% @photos.each_slice(5) do |slice|  -%>
4:  <%  slice.each do %>
5: <h2><%= link_to slice.title,
6: photo_path(:user_id => photo.user, :id => photo) %></h2>
7: <p>
8:


and i tried to modify with :

<% rows = @photos.in_groups_of(5)%>
<% rows.each do |row| %> 
  <tr>
  <% row.each do |photo| %>
    <td>
    <%= image_tag(url_for({:action => 'image', :id => photo.id}),:width => '10%',:height => '10%' ) %>
    </td>
  <% end %>
  </tr>
<% end %>


error :

RuntimeError in Photos#index

Showing photos/index.html.erb where line #6 raised:

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

Extracted source (around line #6):

3:   <tr>
4:   <% row.each do |photo| %>
5:     <td>
6:      <%= image_tag(url_for({:action => 'image', :id => photo.id}),:width => '10%',:height => '10%' ) %>
7:     </td>
8:   <% end %>
9:   </tr>

was i wrong ?

Kuya
A: 

Hi Kuya,

It looks as though you've run into the problem I mentioned in my original answer:

"Be aware that if you don't have 5 photos on your final row, array#in_groups_of will pad it with nil unless you specify an alternative default."

Try this:

<tr>
<% row.each do |photo| %>
  <td>
    <% if photo %>
      <%= image_tag(url_for({:action => 'image', :id => photo.id}),:width => '10%',:height => '10%' ) %>
    <% end %>
  </td>
<% end %>
</tr>
Rick Grundy
haha now i understand what u mean thanks for answer
Kuya
A: 
Kuya
i want 5 pic per row.. but isnt working
Kuya
A: 

Hi Kuya,

I changed the code a little bit here

slice.each do |photo|
   # photo.name, photo.title...
end
ez