我正在尝试用导轨构建一个简单的购物车,现在我可以将产品添加到购物车,我想知道如何在购物车中编辑产品,我正在使用会话来控制购物车中的产品。这是用户添加到购物车时看到的内容:
<% @cart.items.each do |item| %>
<tr>
<td>
<%= image_tag item.pic , :alt => "#{item.title}" %>
</td>
<td>
<%= link_to "#{item.title}" , store_path(item.product_id) %>
</td>
<td>
<%= item.unit_price %>
</td>
<td>
<%= item.quantity %>
</td>
<td>
<%= item.total_price %>
</td>
<% end %>
</tr>
这是 CartItem 类:
class CartItem
attr_reader :product, :quantity
def initialize(product)
@product = product
@quantity = 1
end
def increment_quantity
@quantity += 1
end
def product_id
@product.id
end
def title
@product.name
end
def pic
@pic = @product.photo.url(:thumb)
end
def unit_price
@product.price
end
def total_price
@product.price * @quantity
end
end
我想让用户能够编辑产品数量或删除产品,而不仅仅是清除整个购物车。我怎样才能做到这一点 ?