In this episode, we focus on displaying all of a user's live products on their marketplace homepage. We start by creating a couple of products: one that's set to "live" and another that's not, to make sure our logic works correctly. After setting up these test products, we start building out the logic to actually fetch and show them on the homepage.
First, we ensure that the user's products are being passed down from the controller to the view. We add some simple layout code to check if there are any products, and if so, we loop through them and display each one as a list item. If there aren't any products, we show a friendly message instead.
Right now, both products show up—even the one that's not live. To fix that, we write a scope on the Product model (a scopeLive
method) that allows us to easily filter only for products that are live. This makes it reusable later elsewhere in our app, and also keeps the code clean.
We finish by quickly updating our view so that only live products are listed and style the links a little. At the end, the homepage correctly shows only the user's live products for sale, with each product name as a link (which we'll wire up soon).