Iteration D3: Adding a Button

Now that that’s done, it’s time to add an Add to Cart button for each product.

We don’t need to create a new controller 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. (new may sound similar, but its use is to get a form that’s used to solicit input for a subsequent create action.)

Once this decision is made, the rest follows. What are we creating? Certainly not a Cart or even a Product. What we’re creating is a LineItem. Looking at the comment associated with the create method in app/controllers/line_items_controller.rb, you see that this choice also determines the ...

Get Agile Web Development with Rails 7 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.