February 2020
Intermediate to advanced
496 pages
12h 36m
English
The customer has one last request. Right now, even carts with nothing in them are displayed in the sidebar. Can we arrange for the cart to appear only when it has some content? But of course!
In fact, we have a number of options. The simplest is probably to include the HTML for the cart only if the cart has something in it. We could do this totally within the _cart partial:
| » | <% unless cart.line_items.empty? %> |
| | <h2>Your Cart</h2> |
| | <table> |
| | <%= render(cart.line_items) %> |
| | |
| | <tr class="total_line"> |
| | <td colspan="2">Total</td> |
| | <td class="total_cell"><%= number_to_currency(cart.total_price) %></td> |
| | </tr> |
| | </table> |
| | |
| | <%= button_to 'Empty cart ... |
Read now
Unlock full access