Finishing the Cart

Finish improving the functionality of the cart.

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 in line 12, to add a button:

Press + to interact
<% if notice %>
<aside id="notice"><%= notice %></aside>
<% end %>
<h2>Your Pragmatic Cart</h2>
<ul>
<% @cart.line_items.each do |item| %>
<li><%= item.quantity %> &times; <%= item.product.title %></li>
<% end %>
</ul>
<%= button_to 'Empty cart', @cart, method: :delete,
data: { confirm: 'Are you sure?' } %>

Modifying the controller

In the controller, let’s ...