レールを使用してシンプルなショッピング カートを作成しようとしています。商品をカートに追加できるようになりました。商品がカートに入っているときに商品を編集する方法を知りたいです。セッションを使用してショッピング カート内の商品を制御しています。カートに追加すると、ユーザーに次のように表示されます。
<% @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
カート全体をクリアするだけでなく、製品の数量を編集したり、製品を削除したりできるようにしたいと考えています。どうやってやるの ?