AWDwR Partial Template Question

The following code uses partial template. My question is: What happened
to the looping of cart
items?

add_to_cart.rhtml

Your Cart
<% for cart_item in @cart.items %> <% end %>
<%= cart_item.quantity %>× <%= h(cart_item.title) %> <%= number_to_currency(cart_item.price) %>
Total <%= number_to_currency(@cart.total_price) %>
<%= button_to "Empty cart", :action => :empty_cart %>

Refactored add_to_cart.rhtml

Your Cart
<%= render(:partial => "cart_item", :collection => @cart.items) %>
Total <%= number_to_currency(@cart.total_price) %>
<%= button_to "Empty cart", :action => :empty_cart %>

_cart_item.rhtml

<%= cart_item.quantity %>× <%= h(cart_item.title) %> <%= number_to_currency(cart_item.price) %> >

On Oct 14, 2006, at 9:31 AM, Bala P. wrote:

The following code uses partial template. My question is: What
happened to the looping of cart

The :collection parameter causes render(0 to invoke the template once
for each item in the collection.

Dave

Hi,
the method
<%= render(:partial => “cart_item”, :collection => @cart.items) %>

iterates a partial render for the :collection content:
for each item the @cart.items collection the partial in
“_cart_item.rhtml” is rendered, in this partial code the variable name
to refer for one item is intended to be called “cart_item”, like the
partial name.

_cart_item.rhtml

<%= cart_item.quantity %>צlt;/td> <%= h(cart_item.title) %> <%= number_to_currency(cart_item.price) %> >

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