[英]Display array element in rails
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", :action=>"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]. 问题出在第二个div中,Rails在@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
. 另外,您不应该使用not uniq
id
。 Here you have got multiple product_left
and product_right
ids. 在这里,您有多个
product_left
和product_right
ID。 That's not good 这不好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.