In the book 'Agile web development with Rails", the authors are building
a store-like app. At some point they add dynamic adding a product with
ajax call.
[code]
<!-- store_controller#index -->
<% form_remote_tag :url => { :action => 'add_to_cart', :id => product }
do %>
<%= submit_tag "Add to Cart" %>
<% end %>
[/code]
[code]
# store_controller.rb
def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@current_item = @cart.add_product(product)
# render xhr request
[/code]
[code]
# model cart.rb / no DB table
def add_to_cart(product)
current_item = @items.find {|item| item.product == product}
if current_item
current_item.increment_quantity
else
current_item = CartItem.new(product)
@items << current_item
end
current_item
end
[/code]
Is there a way to add a text field which will represent the quantity of
the product to add in the cart ? Customer enter quantity in text field,
click Add to cart and the product and quantity is added to the
model/cart.
Thanks in advance
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.