Jomazon Prime — Ruby on Rails

The journey through Flatiron School’s Software Engineering Bootcamp Program continues with the introduction of Ruby on Rails - an incredible framework that streamlines the software development process one thousand percent. As a way to test out the new tools and knowledge that we’ve been given, I decided to model my Rails project after the online shopping website Amazon. My app is called Jomazon Prime (no copyright infringement intended, Mr. Bezos!)

Models & Associations

To start things off, I first decided on what models I wanted to use for the application. The essentials included Buyers, Sellers, Products, and Departments. I also wanted users to be able to review and purchase items, which introduced other models such as Carts, Reviews, Ratings, and Comments. Now that I had decided on all my models, I could focus on determining how they are associated with each other via Active Record associations. I feel that this is arguably the most crucial step in the process, as it provides a high-level overview of your product without actually writing any code.

Rails Generators

Now that we have an idea of how our app will behave, we can go ahead and start building our database, as well as the necessary files for our MVC (Model, View, Controller) application structure. One of the great things about Rails is its intuitive terminal commands that allow developers to use its robust macro methods to generate files and folders for us. For example, in order to generate a resource, we can simply type the following in our terminal:

rails g resource User

This single line of code generates the following files for us:

create    db/migrate/20210124223541_create_users.rbcreate    app/models/user.rbcreate    app/controllers/users_controller.rbcreate    app/views/userscreate    app/assets/stylesheets/users.scss

It also primes our config/routes.rb file with MVC routes that we can use to navigate through our application. We’ll want to repeat this command for each of our models, fix our migration files to build our database, then edit each model class to reflect the associations we’ve previously decided for them. And just like that, we’ve got ourselves the foundation of our new application — whew!

Routes & Controllers

Next, let’s give our app some user functionality. In addition to our model controllers, let’s also create another controller that handles User login/logout. Since HTML does not naturally keep track of states, we can utilize Sessions, which Rails comes readily equipped with. The logic for this can be contained in a file called SessionsController.rb, which looks something like this:

Example usage of a Sessions Controller

With the login system in place, we can start filling in the rest of our controllers, always keeping in mind our RESTful routes. The views are directly affected by how we define the actions in their corresponding controllers, so I like to edit these files simultaneously. We want to keep as little logic in our views as possible, so any queries to the database (like finding a User or a Product) should be done either in the controllers (or in some cases the model itself) and saved to an instance variable, which you can then use in your view. It definitely took some practice for me to really get comfortable with this style of programming, but it was and continues to be incredibly rewarding once I finally got a good grasp on it.

Buyers & Sellers

Jomazon requires 2 types of Users - Buyers and Sellers. Since they share mostly the same table attributes, I decided they should derive from the same User class (rather than having 2 separate models for each). The program is able to distinguish between the two by looking at their ‘account_type’ attribute. Regarding their associations, Sellers have many Products, and Buyers have one Cart. We can define these in the User model by specifying a ‘class_name’ and a ‘foreign_key’, which looks something like this:

User class defining different associations

Rails Form Generators

In order for users to send information to the server and interact with the application with their own input, we’ll need to use forms. Rails has a fantastic way of handling this requirement by supplying us with a ‘form_for’ method. This method accepts a block where you can specify which HTML fields you need for your form. Here’s an example of the form that submits and creates a new product:

Example usage of ‘form_for’ to generate a form for a New Product

As you can see, this form uses different HTML elements, including text_field, text_area, number_field, collection_select, hidden_field, and file_field. If you inspect the page in the browser, you’ll notice that Rails has intelligently transformed all of these into valid HTML DOM elements.

Custom Routes

The logic to handle purchases is contained in my CartsController, however I had to define new custom actions since they don’t follow the regular RESTful routes. When adding custom controller actions, we also need to make sure that Rails knows how to navigate to those actions. We can accomplish this by editing our routes.rb file:

Rails Custom Routing


Rails is an incredibly robust framework, packed with everything you need to create a functional web application. At first, it may seem intimidating to start learning it, but once you’ve familiarized yourself with its various moving parts (and with some practice), it quickly becomes an essential part of your app development kit. I’m excited to continue using Ruby to build new programs, and I look forward to the applications my Flatiron Cohort will create in the following months. On to Phase Four!

Jomazon Prime ~ Ruby on Rails

If you’re interested in viewing or contributing to this project, check out the Github Repo here. Additionally, the video below provides a walkthrough demo of the application:

Software Engineer | React | Redux | Ruby | Rails