Making Prettier Listings

Learn to apply some styling to the existing list of products.

Our customer has one more request. The listing of all the products is ugly. Can we pretty it up a bit? And, while we’re there, can we also display the product image along with the image URL?

Before we get too far, it would be nice if we had a consistent set of test data to work with. We could use our scaffold-generated interface and type data in from the browser. However, if we did this, future developers working on our codebase would have to do the same. If we were working as part of a team on this project, each member of the team would have to enter their own data. It would be nice if we could load the data into our table in a more controlled way. Luckily, we can! Rails has the ability to import seed data.

Adding dummy products

To start, we simply modify the file in the DB directory named seeds.rb.

We then add the code to populate the products table. This uses the create!() method in line 2 of the Product model. The following is an extract from that file. Be warned, this seeds.rb script removes existing data from the products table before loading the new data.

Author’s note: You might not want to run it if you’ve just spent several hours typing your own data into the application!

Get hands-on with 1400+ tech skills courses.