Hi all,
I am implementing the shopping cart. The functionality is like I have a
list of products with fields quantity, product name, unit price and
total price. Quantity field is having a text box to change it. There is
a update button to update the quantity of the product.Now when i update
the quantity of the product it is working only for the first product of
the table. It is not working for other products, because from the view i
am not able to fetch the id of the product whose quantity is to be
changed.
My code is:
<% session[:cart].items.each do |item|
product = item.product
-%>
|
|
<%= 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}');"
%>
|
|
<% end %>
Total: |
<%= fmt_dollars(@cart.total_price) %> |
<% form_tag %>
Can anyone tell me where i am going wrong.
Any help would be greatly appreciated.
Thanks,
Rohit.
If you change this line from:
to:
text_field_tag “items[#{item.id}]”, item.quantity, :id =>
“items_#{item.id}”
it will create a hash of values params[:items] that you can then cycle
through and update your cart items.
e.g. params[:items].each { | id, qty | …update routine… }
HTH,
Nicholas
On Nov 21, 12:34 am, Rohit M. [email protected]
Nicholas H. wrote:
If you change this line from:
to:
text_field_tag “items[#{item.id}]”, item.quantity, :id =>
“items_#{item.id}”
it will create a hash of values params[:items] that you can then cycle
through and update your cart items.
e.g. params[:items].each { | id, qty | …update routine… }
HTH,
Nicholas
On Nov 21, 12:34 am, Rohit M. [email protected]
Hi,
Thanks a lot for replying.
Can you tell me further how to implement the update routine. In
params[:items],
i am getting the id of the item and the quantity.
My update routine is:
def product
product = Product.find(params[:id])
@quantity = request.parameters[:qty]
quantity = @quantity
@cart = find_cart
@cart.add_product(id,qty)
redirect_to(:action =>‘display_cart’)
end.
Can u please let me know how to implement params[:items].each { | id,
qty | …update routine… }
Thanks,
Rohit.
Rohit M. wrote:
Hi,
I am getting the value of item id and quantity using the above method.
In my controller in writing the update method as:
def update
params[:items].each do |id, qty|
item= item.find(id)
end
This line item=item.find(id) gives an error.
Now this is the item id i.e item in the session. If i use product =
Product.find(params[:id]). It again throws an error which is obvious coz
it doesn’t have a product with item id in the product table.
How should I proceed further.
Please do let me know.
Hi,
View is:
text_field_tag “items[#{product.id}]”, item.quantity, :id =>
“items_#{product.id}”
My update method is:
params[:items].each do|id,qty|
product = Product.find_by_id(id)
But since item is a session, everytime product is fetching last value of
the session. cab anyone tell me what needs to be done???
Hi,
I am getting the value of item id and quantity using the above method.
In my controller in writing the update method as:
def update
params[:items].each do |id, qty|
item= item.find(id)
end
This line item=item.find(id) gives an error.
Now this is the item id i.e item in the session. If i use product =
Product.find(params[:id]). It again throws an error which is obvious coz
it doesn’t have a product with item id in the product table.
How should I proceed further.
Please do let me know.