Newbie question about adding product to cart

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

David C. 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.

Mohit S. wrote:

David C. 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs