每个循环的条件是当前循环与前一个循环不匹配

时间:2015-11-03 02:59:00

标签: ruby-on-rails

我有一个像这样的控制器:

products_controller.rb

def index
@products = Product..where('order_date BETWEEN ? AND ?', DateTime.now.beginning_of_day, DateTime.now.end_of_day).all

#[id, order_date, name, category]
end

index.html.erb

<% @products.each do |product| %>
<thead>
 <th>
   <%= product.category %>
 </th>
</thead>
<tbody>
 <tr>
  <%= product.name %>
 </tr>
</tbody>

# This code loop through each product to display its product category and the product name. 

如何定义条件,以便product.category等于之前的product.category,然后将product.name追加到<tbody>

0 个答案:

没有答案