I am trying to build a simple shopping cart with rails, now I am able to add products to cart, I want to know how can I edit products while they are in cart, I am using sessions to control the products in shopping cart. here is what the user see when add to cart :
<% @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>
and this is CartItem class :
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
I want to give the user this ability to edit the quantity of products or remove a product, not only clear the whole cart. how can i do that ?