It’s not a lie. Ruby on Rails is truly the magical unicorn, the pot of gold at the end of the rainbow, the elusive feeling of Nirvana you’ve been searching for all your life. Too much? Fine, I’ll stop. But what if I told you that I can create my own fully functional web app from scratch in about half an hour? (or less, depending on how many times you get distracted)
With the help of Rails, anything is possible. I want to make a web app for my latest business venture, Cat Café. Cat Café is a place in which cats can go for brunch and get served and worshipped by humans. Keep in mind this is a belongs-to/has-many relationship. The cat is a client who has many staff waiting on her/him and the server belongs to one cat.
Let’s get started by creating a new Rails app.
Next, we should set up our controllers, models, migration tables, view directories, and routes. Whoa, whoa. Hold up. It sounds like a lot to do, right? That’s where Rails comes in. By doing “rails g resource,” all these files are automatically generated for us. Rails does the majority of the hard, repetitive work. That way, us developers can instead devote our time focused on the content of our app and ensuring it works the way we desire.
Let’s check our migration tables and routes file to make sure everything is correct.
Looks great! Don’t forget we need to set up the relationships between the Cat and Server models.
Next step is to go to our Cats Controller file and set up the methods we want in our our app.
The CEO of Cat Café (me) will be able to see all the cat clientele (index), each cat’s personal profile(show), add new cat clients (new, create), edit an existing cat’s information(edit, update), and delete a cat client(destroy).
Similarly for the Servers Controller file:
I will be able to view all my current staff (index), see a staff member’s profile (show), hire new staff (new, create), edit the current staff’s info (edit, update), and fire a staff member (destroy).
We need to create the view files for each controller.
The “index” pages will display a list of all the cat clients and staff members.
The “edit” and “new” forms will be the same for each model.
The “show” pages for the cat and the staff member are presented as their profiles and will thus show information about them.
Now that we got the chunk of our coding done, let’s go to the seeds.rb file and create some seeds so we have data to display on the app.
It’s almost time to launch our app! Before we do so, we first have to create a database.
Next, run our migrations.
Then, run our seeds.
Finally, let’s fire up the rails server! ⚡️💥
Go to the browser and enter localhost:3000/servers.
Woohoo! It works! 🍾🎈 This is the index page that displays all of the servers working in Cat Café. We can also click on each staff member’s name to view their individual profiles.
In each staff profile, we can find out who their current cat client is, edit the profile, or fire them (which deletes their profile). We can also click on the cat’s name to view their profile as well.
On the cat’s show page, we can view their info, see who works for them, edit their profile, and delete it. We can also click on their staff’s name to view their profile.
Cheshire decided he was bored of eating the avocado tuna toast at our café and he no longer wants to be a client here. Let’s delete his profile.
Sad to see him go, but Cat Café is the hottest place on the East Coast right now. We have a new customer already! Let’s add Oliver to our clientele.
More customers mean we need more staff ASAP! Let’s hire another server.
Since Tony no longer has a cat client, we need to edit his profile and assign him another cat to wait on.
Tony was heartbroken by Cheshire leaving, so much that it affected his work performance. Sorry, but it’s time for us to let Tony go.
There we have it! A fully operational web app that can perform basic CRUD (create, read, update, delete) functions! Yes, visually, it is minimal with no styling. I promised you a working app, not a beautiful one. That’s what CSS is for!