Making Button Functional
Understand how to enable button functionality in Rails by modifying the create method to add products to the shopping cart. Explore how to use session management, controller callbacks, and parameter handling to build and redirect line items effectively.
We'll cover the following...
Before we push the button, we need to modify the create() method in the line items controller to expect a product ID as a form parameter. Here’s where we start to see how important the id field is in our models. Rails identifies model objects, and their corresponding database rows, by their id fields. If we pass an ID to create(), we’re uniquely identifying the product to add.
Why the create() method? The default HTTP method for a link is a GET, and for a button is a POST. Rails uses these conventions to determine which method to call. Refer to the comments inside the app/controllers/line_items_controller.rb ...