Search⌘ K
AI Features

Finishing the Cart

Explore how to finish the cart functionality in a Rails e-commerce app by adding empty cart actions, updating controller methods, and improving cart display with tables and styling. This lesson helps you handle user sessions, manage notifications, and enhance the cart’s presentation for a polished application.

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:

HTML
<% 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 modify the destroy() ...