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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access