Search⌘ K
AI Features

Adding a Button

Understand how to add an Add to Cart button for each product in your Rails 6 application. Learn to use the scaffold-generated create action and button_to helper to handle POST requests, passing product IDs correctly to update the shopping cart. This lesson also covers customizing the button's appearance with CSS for better user experience.

Now it’s time to add an “Add to Cart” button for each product.

Using a Scaffold generator

We don’t need to create a new controller (LineItem) or even a new action. Taking a look at the actions provided by the scaffold generator, we find index(), show(), new(), edit(), create(), update(), and destroy(). The one that matches this operation is create(). The new() action may sound similar, but it is used to get a form that’s used to ...