Bob Wickham
2006-Jun-26 03:45 UTC
[Rails] Newbie question about Agile Rails book - Delete from cart
I have been working through the Agile Web Development with Rails book doing the Depot tutorial. I have completed it and it works but I cant say I understand how and why. My question is this "After adding products to the Cart, how do I then give users the option to delete just one item and not empty the whole cart?" I''ve been playing around with all sorts of code, just making myself more confused. Any suggestions very much appreciated Bob -- Posted via http://www.ruby-forum.com/.
Sean O''Hara
2006-Jun-26 04:02 UTC
[Rails] Newbie question about Agile Rails book - Delete from cart
Hi Bob, If you don''t understand most of the tutorial then I think it you should consider following some other tutorials or better yet designing your own app... that will force you to understand what you are doing. Don''t worry, you''re not alone, it''s taken me a long time to wrap my head around RonR. Anyway, to remove an item, I added this to my controller: def remove_item @cart = find_cart @cart.remove_cart_item(params[:id]) end And this to the cart model: # Method to remove a product/option from the cart def remove_cart_item(id) if @items.delete_if {|item| item.id == id.to_i } true else false end end And finally I modified the initialize function in the CartItem class like so, so that each item in the @items array of the cart object would have a unique id: def initialize(product,option) @product = product unless product.nil? @option = option unless option.nil? @quantity = 1 @id = Time.now.to_i end Let me know if you don''t know what any of this code is doing. Basically it just removes the appropriate item from the @items array in the cart object based on the item id. Take care, Sean On 25-Jun-06, at 11:45 PM, Bob Wickham wrote:> I have been working through the Agile Web Development with Rails book > doing the Depot tutorial. I have completed it and it works but I cant > say I understand how and why. > My question is this "After adding products to the Cart, how do I then > give users the option to delete just one item and not empty the whole > cart?" > I''ve been playing around with all sorts of code, just making myself > more > confused. > Any suggestions very much appreciated > > Bob > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Bob Wickham
2006-Jun-26 04:52 UTC
[Rails] Re: Newbie question about Agile Rails book - Delete from car
Hi Sean, Thanks for that. I understand what your code is doing which is about where I am with most of Rails. How to get it to work is another matter. I assume by this> And finally I modified the initialize function in the CartItem class > like so, so that each item in the @items array of the cart object > would have a unique id: > > def initialize(product,option) > @product = product unless product.nil? > @option = option unless option.nil? > @quantity = 1 > @id = Time.now.to_i > endyou mean the initialize function in Cart.rb as you say "modify" and thats the only occurrence of initialize. Can you suggest how to code a ''remove item'' link on the display_cart.rhtml page. Bob -- Posted via http://www.ruby-forum.com/.
Sean O''Hara
2006-Jun-26 05:34 UTC
[Rails] Re: Newbie question about Agile Rails book - Delete from car
Hi Bob, Sure, for each row in the table produced on the display cart page you can just add another column at the end with a link in it. So you just need to pass the correct action an id to the link_to helper. The initialize method I''m referring to is in the CartItem class, not the Cart class. If you don''t have a CartItem class yet I guess it means it makes it''s appearance a bit later in the tutorial. Take care, Sean On 26-Jun-06, at 12:51 AM, Bob Wickham wrote:> Hi Sean, > Thanks for that. > I understand what your code is doing which is about where I am with > most > of Rails. How to get it to work is another matter. > I assume by this > >> And finally I modified the initialize function in the CartItem class >> like so, so that each item in the @items array of the cart object >> would have a unique id: >> >> def initialize(product,option) >> @product = product unless product.nil? >> @option = option unless option.nil? >> @quantity = 1 >> @id = Time.now.to_i >> end > > you mean the initialize function in Cart.rb as you say "modify" and > thats the only occurrence of initialize. > > Can you suggest how to code a ''remove item'' link on the > display_cart.rhtml page. > > Bob > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Bob Wickham
2006-Jun-26 05:51 UTC
[Rails] Re: Re: Newbie question about Agile Rails book - Delete from
Sean O''Hara wrote:> Hi Bob, > > Sure, for each row in the table produced on the display cart page you > can just add another column at the end with a link in it. So you just > need to pass the correct action an id to the link_to helper. > > The initialize method I''m referring to is in the CartItem class, not > the Cart class. If you don''t have a CartItem class yet I guess it > means it makes it''s appearance a bit later in the tutorial. > > Take care, > SeanMaybe we''re reading from different books, mines printed February 2006, as I''m up to the chapter on Testing and there isn''t any mention of CartItem (but I do have a LineItem class). I''ve looked through the completed code in the back of the book to make sure I haven''t missed anything. Anyway, thanks for your advice. I''ll have a go at including it in my code. Bob -- Posted via http://www.ruby-forum.com/.
Sean O''Hara
2006-Jun-26 15:25 UTC
[Rails] Re: Re: Newbie question about Agile Rails book - Delete from
Hi Bob, Yep, I was referring the the 2nd edition, which is currently available in beta form. There''s a lot of improvements an additions already in the beta of the 2nd edition. I think it''s worth upgrading. Take care, Sean On 26-Jun-06, at 1:50 AM, Bob Wickham wrote:> Sean O''Hara wrote: >> Hi Bob, >> >> Sure, for each row in the table produced on the display cart page you >> can just add another column at the end with a link in it. So you just >> need to pass the correct action an id to the link_to helper. >> >> The initialize method I''m referring to is in the CartItem class, not >> the Cart class. If you don''t have a CartItem class yet I guess it >> means it makes it''s appearance a bit later in the tutorial. >> >> Take care, >> Sean > > Maybe we''re reading from different books, mines printed February 2006, > as I''m up to the chapter on Testing and there isn''t any mention of > CartItem (but I do have a LineItem class). I''ve looked through the > completed code in the back of the book to make sure I haven''t missed > anything. > Anyway, thanks for your advice. I''ll have a go at including it in my > code. > > Bob > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails