# How to add multiple items to a cart

**URL:** <https://www.ruby-forum.com/t/how-to-add-multiple-items-to-a-cart/117111>\
**Category:** Rails\
**Created:** [October 13, 2007, 2:53am UTC](https://www.ruby-forum.com/t/how-to-add-multiple-items-to-a-cart/117111 "2007-10-13T02:53:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [October 13, 2007, 2:53am UTC](https://www.ruby-forum.com/t/how-to-add-multiple-items-to-a-cart/117111/1 "2007-10-13T02:53:44Z")

</div>

My first question here.  
I am developing a shopping cart based on “Beginning Ruby on Rails E-  
Commerce”.  
I would like to be able to add multiple items. Because, what if I  
wanted 700 items? I don’t really want to repeat clicking on add or  
dragging it to the cart.  
I think the best thing (maybe) would be to add a text box in the cart  
for item.amount but I’m not sure how to do this and I’m worried I’ll  
mess up the code.  
Any ideas?  
Please please help.

My cart.rb model:  
def total  
cart\_items.inject(0) {|sum, n| n.price \* n.amount + sum}  
end

def add(product\_id)  
items = cart\_items.find\_all\_by\_product\_id(product\_id)  
product = Product.find(product\_id)

if items.size \< 1  
ci = cart\_items.create(:product\_id =\> product\_id,  
:amount =\> 1,  
:price =\> product.price)  
else  
ci = items.first  
ci.update\_attribute(:amount, ci.amount + 1)  
end  
ci  
end

def remove(product\_id)  
ci = cart\_items.find\_by\_product\_id(product\_id)

```
if ci.amount > 1
  ci.update_attribute(:amount, ci.amount - 1)
else
  CartItem.destroy(ci.id)
end
return ci

```

end

My cart\_controller.rb has:  
def add  
params[:id].gsub!(/product\_/, “”)  
@product = Product.find(params[:id])

if request.xhr?  
@item = @cart.add(params[:id])  
flash.now[:cart\_notice] = “Added _#{@item.product.title}\</  
em\>”  
render :action =\> “add\_with\_ajax”  
elsif request.post?  
 @item = @cart.add(params[:id])  
flash[:cart\_notice] = “Added #{@item.product.title}”  
redirect\_to :controller =\> “catalog”  
else  
render  
end  
end_

my \_cart.rhtml view has:

 \<% for item in @cart.cart\_items %\> - \<%= render :partial =\> "cart/item", :object =\> item %\> 
 \<% end %\>

add\_with\_ajax.rjs:  
page.replace\_html “shopping\_cart”, :partial =\> “cart”  
page.visual\_effect :highlight,  
“cart\_item\_#{@item.product.id}”, :duration =\> 3  
page.visual\_effect :fade, ‘cart\_notice’, :duration =\> 3

add.rhtml:  
**Please confirm adding _\<%= @product.title %\>_  
to your shopping cart.**  
\<%= button\_to “Confirm”, :action =\> “add”, :id =\> params[:id] %\>

Thanks,  
Elle

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [October 17, 2007, 5:26pm UTC](https://www.ruby-forum.com/t/how-to-add-multiple-items-to-a-cart/117111/2 "2007-10-17T17:26:18Z")

</div>

Hi Elle,

The first step is to get your model to allow adding many items with a  
specified amount. In card.rb:

def add(product\_id, amount = 1) # By setting the default to one,

# if no amount is specified, the method is still the same

items = cart\_items.find\_all\_by\_product\_id(product\_id)  
product = Product.find(product\_id)

if items.size \< 1  
ci = cart\_items.create(:product\_id =\> product\_id,  
:amount =\> amount,  
:price =\> product.price)  
else  
ci = items.first  
ci.update\_attribute(:amount, ci.amount + amount)  
end  
ci  
end

Now we need to get it to the controller action. Change  
@item = @cart.add(params[:id])  
to  
@item = @cart.add(params[:id], params[:amount])

Last step is to add a text box to the form. It looks like it would go  
in the cart/item partial (no listed here?) but basically needs to look  
like this inside your form:

\<%= text\_box\_tag “amount”, 1 %\>

Good Luck,  
Rob
