I try to make the two columns list of products, this is my code
<table>
<% (0..@products.length-1).step(2) do |i| %>
<tr><td><div id="product_left">
<%= image_tag @products[i].photo.url(:small) %>
<%= @products[i].name %><br/>
Price: <%= @products[i].price %><br/>
<%= link_to "details", :action=>"show", :id=> @products[i].id %>
<%= link_to "add to card", {:controller=> "carts", :act开发者_开发问答ion=>"add", :id=> @products[i].id}, :remote=> true %>
</div> </td>
<% if @products.length > i %>
<td><div id="product_right">
<%= image_tag @products[i+1].photo.url(:small) %>
<%= @products[i+1].name %><br/>
Price: <%= @products[i+1].price %><br/>
<%= link_to "details", :action=>"show", :id=> @products[i+1].id %>
<%= link_to "add to card", {:controller=> "carts", :action=>"add", :id=> @products[i+1].id}, :remote=> true %>
</div></td>
<% end %>
</tr>
<% end %>
</table>
the problem is in the second div, rails give me an error on @products[i+1]. How can I solve it?
<table>
<% @products.each_slice(2) do |products| -%>
<tr>
<% products.zip(["left", "right"]).each do |product, side| -%>
<td>
<div id="product_<%= side %>">
<%= image_tag product.photo.url(:small) %>
<%= product.name %><br/>
Price: <%= product.price %><br/>
<%= link_to "details", product %>
<%= link_to "add to card", [:add, :carts, product], :remote=> true %>
</div>
</td>
<% end %>
</tr>
<% end -%>
</table>
Also you shouldn't use not uniq id
. Here you have got multiple product_left
and product_right
ids. That's not good
精彩评论