# Shopping cart implementation

**URL:** <https://www.ruby-forum.com/t/shopping-cart-implementation/120739>\
**Category:** Rails\
**Created:** [November 20, 2007, 9:29am UTC](https://www.ruby-forum.com/t/shopping-cart-implementation/120739 "2007-11-20T09:29:22Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [November 20, 2007, 9:29am UTC](https://www.ruby-forum.com/t/shopping-cart-implementation/120739/1 "2007-11-20T09:29:22Z")

</div>

Hi all,

I am implementing the functionality of shopping cart.  
The problem is there is a textbox for updating quantity of the product  
on the same page where the products are displayed after ading in the  
cart. By default the quantity is one and after that the user can change  
the quantity of the product.

Its like:  
Qty Product Price Total

textbox. abc $10.00 $10.00 remove product

Total: $10.00

My problem is that I am not able to update the implement this  
functionality. Can anyone give me some idea of how to implement it??

Thanks,  
Ruchita S…

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [November 20, 2007, 9:38am UTC](https://www.ruby-forum.com/t/shopping-cart-implementation/120739/2 "2007-11-20T09:38:17Z")

</div>

Ruchita S. wrote:

> Hi all,
> 
> I am implementing the functionality of shopping cart.  
> The problem is there is a textbox for updating quantity of the product  
> on the same page where the products are displayed after ading in the  
> cart. By default the quantity is one and after that the user can change  
> the quantity of the product.
> 
> Its like:  
> Qty Product Price Total
> 
> textbox. abc $10.00 $10.00 remove product
> 
> Total: $10.00 update button for quantity.
> 
> My problem is that I am not able to update the implement this  
> functionality. Can anyone give me some idea of how to implement it??
> 
> Thanks,  
> Ruchita S…  
> I have written a code like:  
> store\_controller.rb  
> def update  
> @quantity = request.parameters[:quantity]  
> @product = request.parameters[:productid]  
> quantity = @quantity  
> #productname=@product  
> product = @product  
> @cart = find\_cart  
> @cart.add\_product(product,quantity)  
> render :action =\> ‘display\_cart’  
> end

cart.rb  
def add\_product(product,quantity)  
#@items \<\< Lineitem.for\_product(product)  
item = @items.find {|i| i.product\_id == product.id }  
if item  
item.quantity += 1  
else  
item = Lineitem.for\_product(product)  
@items \<\< item  
end

But the above code is throwing error:

ActiveRecord::AssociationTypeMismatch in StoreController#update

Product expected, got String

Can anyone tell me why it’s throwing this error???

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [November 20, 2007, 10:14am UTC](https://www.ruby-forum.com/t/shopping-cart-implementation/120739/3 "2007-11-20T10:14:08Z")

</div>

On 20 Nov 2007, at 08:38, Ruchita S. wrote:

> But the above code is throwing error:
> 
> ActiveRecord::AssociationTypeMismatch in StoreController#update
> 
> Product expected, got String

presumably because @product is a string containing a product id,  
rather than an instance of Product.

Fred

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [November 20, 2007, 10:29am UTC](https://www.ruby-forum.com/t/shopping-cart-implementation/120739/4 "2007-11-20T10:29:15Z")

</div>

Frederick C. wrote:

> On 20 Nov 2007, at 08:38, Ruchita S. wrote:
> 
> > But the above code is throwing error:
> > 
> > ActiveRecord::AssociationTypeMismatch in StoreController#update
> > 
> > Product expected, got String
> 
> presumably because @product is a string containing a product id,  
> rather than an instance of Product.
> 
> Fred

yeah I got that. Can you tell me what is the way to implement it?

store\_controller.rb  
def add\_to\_cart  
product = Product.find(params[:id])  
quantity = 1  
@cart = find\_cart  
@cart.add\_product(product,quantity)  
redirect\_to(:action =\>‘display\_cart’)  
rescue  
logger.error(“Attempt to access invalid product #{params[:id]}”)  
flash[:notice] =‘Invalid product’  
redirect\_to(:action =\> ‘index’)  
end

def update  
@quantity = request.parameters[:quantity]  
@product = request.parameters[:productid]  
quantity = @quantity  
product = @product  
add\_to\_cart  
end

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [November 20, 2007, 10:50am UTC](https://www.ruby-forum.com/t/shopping-cart-implementation/120739/5 "2007-11-20T10:50:38Z")

</div>

Frederick C. wrote:

> On 20 Nov 2007, at 09:29, Ruchita S. wrote:
> 
> > > presumably because @product is a string containing a product id,  
> > > rather than an instance of Product.
> > > 
> > > Fred
> > 
> > yeah I got that. Can you tell me what is the way to implement it?
> 
> Well you say you’ve got it, but you’re just not doing it.
> 
> > def update  
> > @quantity = request.parameters[:quantity]  
> > @product = request.parameters[:productid]
> 
> you’re just setting @product to a string rather than using Product.find
> 
> Fred

Hi,

The code is working now but only for the first row of the table i.e only  
for the first product.  
My code is:  
Store\_controller.rb  
def add\_to\_cart  
product = Product.find(params[:id])  
quantity = 1  
@cart = find\_cart  
@cart.add\_product(product,quantity)  
redirect\_to(:action =\>‘display\_cart’)  
rescue  
logger.error(“Attempt to access invalid product #{params[:id]}”)  
flash[:notice] =‘Invalid product’  
redirect\_to(:action =\> ‘index’)  
end

def update  
@quantity = request.parameters[:quantity]  
product = Product.find(params[:productid])  
quantity = @quantity  
@cart = find\_cart  
@cart.add\_product(product,quantity)  
redirect\_to(:action =\>‘display\_cart’)  
end

cart.rb  
def add\_product(product,quantity)  
#@items \<\< Lineitem.for\_product(product)  
item = @items.find {|i| i.product\_id == product.id }  
if item  
item.quantity += 1  
else  
item = Lineitem.for\_product(product,quantity)  
@items \<\< item  
end

lineitem.rb

def self.for\_product(product,quantity)  
item = self.new  
item.quantity = quantity  
item.product = product  
item.unit\_price = product.product\_price  
item  
end

Can you tell me wat is wrong here.

Thanks a lot for the help.

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [November 20, 2007, 10:42am UTC](https://www.ruby-forum.com/t/shopping-cart-implementation/120739/6 "2007-11-20T10:42:51Z")

</div>

On 20 Nov 2007, at 09:29, Ruchita S. wrote:

> > presumably because @product is a string containing a product id,  
> > rather than an instance of Product.
> > 
> > Fred
> 
> yeah I got that. Can you tell me what is the way to implement it?

Well you say you’ve got it, but you’re just not doing it.

> def update  
> @quantity = request.parameters[:quantity]  
> @product = request.parameters[:productid]

you’re just setting @product to a string rather than using Product.find

Fred

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [November 20, 2007, 12:54pm UTC](https://www.ruby-forum.com/t/shopping-cart-implementation/120739/7 "2007-11-20T12:54:34Z")

</div>

Ruchita S. wrote:

> Hi,
> 
> Can anyone tell me what’s wrong with the above code. Th code is working  
> but only for first product in the cart and not for other ones.

Hi,

Please tell me the solution of this problem.

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [November 20, 2007, 12:14pm UTC](https://www.ruby-forum.com/t/shopping-cart-implementation/120739/8 "2007-11-20T12:14:59Z")

</div>

Hi,

Can anyone tell me what’s wrong with the above code. Th code is working  
but only for first product in the cart and not for other ones.

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [November 20, 2007, 1:57pm UTC](https://www.ruby-forum.com/t/shopping-cart-implementation/120739/9 "2007-11-20T13:57:36Z")

</div>

Ruchita, I guess you might find rails irc channel a lot more useful  
than this mailing list, as it’s real time.

Try [http://www.mirc.com/](http://www.mirc.com/)  
Server : [irc.freenode.net](http://irc.freenode.net)  
Channel : #rubyonrails

On Nov 20, 2007 11:54 AM, Ruchita S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Cheers!

- Pratik  
[http://m.onkey.org](http://m.onkey.org)

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [November 22, 2007, 12:33pm UTC](https://www.ruby-forum.com/t/shopping-cart-implementation/120739/10 "2007-11-22T12:33:55Z")

</div>

HI,

I am writing

\<%= text\_field\_tag "items[#{product.id}]", item.quantity, :id =\>"items\_#{product.id}"%\> in my view.

In controller  
def update  
params[:items].each do|id,qty|  
product=Product.find\_by\_id(id)  
end

In params[:items] in am getting parameters like {“items”=\>{“35”=\>“1”,  
“36”=\>“9”}},  
but when i am using this in my controller it is showing the last  
selected product.  
Can anyone tell me how to get the all parameters from  
params[:items].each do |id,qty|.

Thanks,  
Ruchita S…

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [November 21, 2007, 5:41am UTC](https://www.ruby-forum.com/t/shopping-cart-implementation/120739/11 "2007-11-21T05:41:50Z")

</div>

Hi all,

I am implementing the functionality of shopping cart. The problem is  
there is a textbox for updating quantity of the product on the same page  
where the products are displayed after adding in the cart. By default  
the quantity is one and after that the user can change the quantity of  
the product by changing the quantity in the text field and clicking the  
update button. The quantity is updating but only for the first product  
in the table. Not able to get the id of the selected product for  
updating the quantity.

The code is as follows:

\<%= form\_tag :controller =\> ‘store’, :action=\> ‘update’%\>

\<% i = 0 for item in @items product = item.product i = i + 1 -%\> \<% end %\> \<% form\_tag %\>

| Qty | Description | Price |
| Each | Total |
| | \<%= text\_field('id\_'+i.to\_s, 'product', 'id') %\> | \<%= h(product.product\_name) %\> | \<%= fmt\_dollars(item.unit\_price) %\> | \<%= fmt\_dollars(item.unit\_price \* item.quantity) %\> | \<%= link\_to\_remote "remove product", :update =\> "tablecart", :url =\> {:action =\> :remove\_from\_cart, :controller =\> "store",:id =\> product.id}, :complete =\> "deductprice('#{item.unit\_price}','#{@cart.total\_price}','#{product.id}');" %\> | |
| **Total:** | \<%= fmt\_dollars(@cart.total\_price) %\> |

Can someone tell me where I am doing mistake in the above code?

Thanks,  
Ruchita.
