# How to change quantity to cart?

**URL:** <https://www.ruby-forum.com/t/how-to-change-quantity-to-cart/170412>\
**Category:** Rails\
**Created:** [June 17, 2009, 10:18am UTC](https://www.ruby-forum.com/t/how-to-change-quantity-to-cart/170412 "2009-06-17T10:18:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike75](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac8455/32.png) [@Mike75](https://www.ruby-forum.com/u/Mike75)\
**Post date:** [June 17, 2009, 10:18am UTC](https://www.ruby-forum.com/t/how-to-change-quantity-to-cart/170412/1 "2009-06-17T10:18:14Z")

</div>

hi all…  
I’ve been studying ROR using the agile development ROR book.  
and I’m stuck at the shopping cart.

what I am trying to do is that, from the book, the ‘add\_to\_cart’  
button doesn’t take the quantity from the users.

well, What I am trying to do is that allowing users to input quantity  
value  
before pressing ‘add\_to\_cart’ and when ‘add\_to\_cart’ is pressed, the  
users can see  
the input values with the total price before checking out the items.

So, on the view, I added the following line.

Qty : \<%= text\_field\_tag ‘quantity’, ‘1’, :size =\> 2, :maxlength =\> 2  
%\>

\<% form\_remote\_tag :url =\> {:action =\> ‘add\_to\_cart’, :id =\> product }  
do %\>  
\<%= submit\_tag “Add to Cart” %\>  
\<% end %\>

and the store\_controller,

quantity = Integer(params[‘quantity’])

well…  
the flashing cart shows 0 quantity.  
i need help, please?

///////////////  
Your Cart  
0(quantity) × 1(prod\_id) $0.00  
Total $0.00  
////////////////

--------------------------- (controller)  
def add\_to\_cart  
begin  
product = Product.find(params[:id])  
quantity = Integer(params[‘quantity’]) #------ i hope this is  
right way to get quantity from the view

rescue ActiveRecord::RecordNotFound  
logger.error(“Attempt to access invalid product #{params[:id]}” )  
redirect\_to\_index(“Invalid product” )  
else  
@cart = find\_cart  
@current\_item = @cart.add\_product(product, quantity)  
respond\_to do |format|  
format.js if request.xhr?  
format.html {redirect\_to\_index}  
end  
end  
end

--------------------------- (cart\_item.rb)  
def initialize(product, quantity)  
@product = product  
@quantity = quantity  
end  
def increment\_quantity(quantity)  
@quantity += quantity  
end  
def price

#### product has a p200 field with the price in it

```
@product.p200 * @quantity

```

end

-----------------------------(cart.rb)  
def add\_product(product, quantity)  
current\_item = @items.find {|item| item.product == product}  
if current\_item  
current\_item.increment\_quantity(quantity)  
else  
current\_item = CartItem.new(product, quantity)  
@items \<\< current\_item  
end  
current\_item  
end

def total\_price  
@items.sum { |item| item.price }  
end

```
    def total_items
           @items.sum { |item| item.quantity }
   end
```

---

<div class="post-metadata">

**Author:** ![Mike75](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac8455/32.png) [@Mike75](https://www.ruby-forum.com/u/Mike75)\
**Post date:** [June 17, 2009, 4:18pm UTC](https://www.ruby-forum.com/t/how-to-change-quantity-to-cart/170412/2 "2009-06-17T16:18:40Z")

</div>

The code you provided in your form:

Qty : \<%= text\_field\_tag ‘quantity’, ‘1’, :size =\> 2, :maxlength =\> 2  
%\>  
\<% form\_remote\_tag :url =\> {:action =\> ‘add\_to\_cart’, :id =\>  
product }  
do %\>  
\<%= submit\_tag “Add to Cart” %\>  
\<% end %\>

You need to have the quantity field within the form:

Qty :  
\<% form\_remote\_tag :url =\> {:action =\> ‘add\_to\_cart’, :id =\>  
product }  
do %\>  
\<%= text\_field\_tag ‘quantity’, ‘1’, :size =\> 2, :maxlength =\> 2  
%\>  
\<%= submit\_tag “Add to Cart” %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![Mike75](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac8455/32.png) [@Mike75](https://www.ruby-forum.com/u/Mike75)\
**Post date:** [June 18, 2009, 2:35am UTC](https://www.ruby-forum.com/t/how-to-change-quantity-to-cart/170412/3 "2009-06-18T02:35:55Z")

</div>

^^ Henry…  
thanx …for checking my codes…  
and pointing out the error…  
it’s working now…

🙂
