June 2025
Intermediate to advanced
488 pages
12h 27m
English
We know by now that to implement the empty-cart function, we have to add a link to the cart and modify the destroy method in the carts controller to clean up the session.
Start with the template and use the button_to method to add a button:
| | <div id="<%= dom_id cart %>"> |
| | <h2 class="font-bold text-lg mb-3">Your Pragmatic Cart</h2> |
| | |
| | <ul class="list-none list-inside"> |
| | <% cart.line_items.each do |item| %> |
| | <li><%= item.quantity %> × <%= item.product.title %></li> |
| | <% end %> |
| | </ul> |
| | </div> |
| | |
| » | <%= button_to 'Empty Cart', cart, method: :delete, |
| » | class: 'ml-4 rounded-lg py-1 px-2 text-white bg-green-600' ... |
Read now
Unlock full access