Hello. I''m using the Agile Web Dev. book 1st Edition and I''m puzzled by the code on pp. 83-84. <%= link_to ''Add to Cart'', {:action => ''add_to_cart'', :id => product }, :class => ''addtocart'' %> def add_to_cart product = Product.find(params[:id]) @cart = find_cart @cart.add_product(product) redirect_to(:action => ''display_cart'') end In the link_to code, why isn''t :id set to product.id? In other words, shouldn''t it be :id => product.id ? I don''t understand why the whole product object is being hashed when the find method is only going to use the id value anyway. Thanks David -- Posted via http://www.ruby-forum.com/.
Mohit Sindhwani
2006-Aug-07 03:02 UTC
[Rails] Newbie question about adding product to cart
David Cheng wrote:> Hello. I''m using the Agile Web Dev. book 1st Edition and I''m puzzled by > the code on pp. 83-84. > > <%= link_to ''Add to Cart'', {:action => ''add_to_cart'', :id => product }, > :class => ''addtocart'' %> > > def add_to_cart > product = Product.find(params[:id]) > @cart = find_cart > @cart.add_product(product) > redirect_to(:action => ''display_cart'') > end > > In the link_to code, why isn''t :id set to product.id? In other words, > shouldn''t it be :id => product.id ? > > I don''t understand why the whole product object is being hashed when the > find method is only going to use the id value anyway. > >Umm, I don''t have the 1st edition (I have the 2nd edition PDF) and it is explained there that when you make that assignment, it actually sets it to product.id (I think it''s due to the conventions involved) and it''s basically "idiomatic shorthand" Hope this helps! Cheers Mohit.
David Cheng
2006-Aug-07 13:27 UTC
[Rails] Re: Newbie question about adding product to cart
Mohit Sindhwani wrote:> David Cheng wrote: >> redirect_to(:action => ''display_cart'') >> end >> >> In the link_to code, why isn''t :id set to product.id? In other words, >> shouldn''t it be :id => product.id ? >> >> I don''t understand why the whole product object is being hashed when the >> find method is only going to use the id value anyway. >> >> > Umm, I don''t have the 1st edition (I have the 2nd edition PDF) and it is > explained there that when you make that assignment, it actually sets it > to product.id (I think it''s due to the conventions involved) and it''s > basically "idiomatic shorthand" > > Hope this helps! > Cheers > Mohit.Yes you''re right - I just saw the footnote in my book that says the same thing. D''oh! David -- Posted via http://www.ruby-forum.com/.