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
On 10/12/07, elle <waznelle-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
> 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
<em>#{@item.product.title}</
> em>"
> render :action => "add_with_ajax"
> elsif request.post?
> @item = @cart.add(params[:id])
> flash[:cart_notice] = "Added
<em>#{@item.product.title}</em>"
> redirect_to :controller => "catalog"
> else
> render
> end
> end
>
> my _cart.rhtml view has:
> <ul>
> <% for item in @cart.cart_items %>
> <li id="cart_item_<%= item.product.id %>">
> <%= render :partial => "cart/item", :object => item
%>
> </li>
> <% end %>
> </ul>
>
>
>
> 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:
> <strong>Please confirm adding <em><%= @product.title
%></em>
> to your shopping cart.</strong>
> <%= button_to "Confirm", :action => "add", :id
=> params[:id] %>
>
> Thanks,
> Elle
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---