Shopping cart implementation

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…

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???

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

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

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.

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

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.

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.

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/
Server : irc.freenode.net
Channel : #rubyonrails

On Nov 20, 2007 11:54 AM, Ruchita S.
[email protected] wrote:


Posted via http://www.ruby-forum.com/.


Cheers!

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…

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.