Making Prettier Listings
Learn to apply some styling to the existing list of products.
We'll cover the following...
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!
Product.delete_allProduct.create!(title: 'Docker for Rails Developers',description:%{<p><em>Build, Ship, and Run Your Applications Everywhere</em>Docker does for DevOps what Rails did for web development—it gives youa new set of superpowers. Gone are “works on my machine” woes and lengthysetup tasks, replaced instead by a simple, consistent, Docker-baseddevelopment environment that will have your team up and running in seconds.Gain hands-on, real-world experience with a tool that’s rapidly becomingfundamental to software development. Go from zero all the way to productionas Docker transforms the massive leap of deploying your app in the cloudinto a baby step.</p>},image_url: 'ridocker.jpg',price: 38.00)# . . .Product.create!(title: 'Build Chatbot Interactions',description:%{<p><em>Responsive, Intuitive Interfaces with Ruby</em>The next step in the evolution of user interfaces is here.Chatbots let your users interact with your service in theirown natural language. Use free and open source tools alongwith Ruby to build creative, useful, and unexpected interactionsfor users. Take advantage of the Lita framework’s step-by-stepimplementation strategy to simplify bot development and testing.From novices to experts, chatbots are an area in which everyonecan participate. Exercise your creativity by creating chatbotskills for communicating, information, and fun.</p>},image_url: 'dpchat.jpg',price: 20.00)# . . .Product.create!(title: 'Programming Crystal',description:%{<p><em>Create High-Performance, Safe, Concurrent Apps</em>Crystal is for Ruby programmers who want more performance or fordevelopers who enjoy working in a high-level scripting environment. Crystalcombines native execution speed and concurrency with Ruby-like syntax, soyou will feel right at home. This book, the first available on Crystal,shows you how to write applications that have the beauty and elegance of amodern language, combined with the power of types and modern concurrencytooling. Now you can write beautiful code that runs faster, scales better,and is a breeze to deploy.</p>},image_url: 'crystal.jpg',price: 40.00)
Note: This code uses
%{...}
. This ...