Iteration F4: Hiding an Empty Cart with a Custom Helper
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', cart, method: :delete, ... |
Get Agile Web Development with Rails 5.1 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.