Couponing with Sinatra.

Chelsea Roland
4 min readJan 8, 2021

Going into my second project while attending flatiron I was a nervous. I pushed the nerves aside and started planning what I wanted my project to look like, and how it would be built.

let’s take a dive straight into it.

First off, I installed the corneal gem.

what is corneal? Corneal is gem created by a former Flatiron student named Brian Emory. it saves us a lot of time by setting up the file structure of the project in only three commands.

Install the gem with gem install corneal

new APP-NAME

Run bundle

Now before I do anything else, I know I will be using an API so I have a readily available list of coupons. I set up an adapter to deal with API requests.

Next, I need to be using an MVC structure (‘models’, ‘views’ and ‘controllers’). Luckily the basic structure is set up by corneal.

It’s time to make my application controller. Any other controller that I create will inherit from this. Within this controller I have enabled sessions. Enabling sessions stores all data in a cookie. I also set “session secret”: a key used for signing and/or encrypting cookies set by the application to maintain session state.

I will add a few more controllers. Coupons, Users, and Sessions. We will look at these more in depth later on.

MAKING TABLES.

I created three tables. For this I used Active Record to automate interactions with the database to quickly build the application.

create coupons: This is where my available coupons will be stored. They all need a store name, code, description and id.

create users: This table will store my users. They all require having a username/email, password, and user id.

Oh no! I had two has many relationship tables. I now need to create a third table joining the coupon table and the user’s table.

Create users coupons: This will join both my tables together this will include user id, and a coupon id.

CONTROLLERS

The controller’s job is to handle all the incoming requests, responses, and routing. I needed to create three controllers.

Coupon controller: will direct the user to the list of available coupons, if they are not logged in however, they will be redirected to the sign-up page. The user can create coupons to add the index of already posted coupons available from the API.

Sessions controller: the sessions controller is pretty straight forward. the user can sign up, where they will need an email and a password, and when they log out the current session will be cleared.

Users controller: this will be in charge of the log in portion. this will also enable the user to save any coupon into a list that they can access at any time, I have labeled this as the user’s index. they can also delete or edit any coupon they have added.

Models

Let’s make some models (where data is manipulated / saved.)

Coupon model: The coupons have a has many relationships with the user. users have many coupons.

user model: The user has a has many relationships with coupons. coupons have many users.

Users coupons model: this table has a belongs to relationship with both the coupons and users table.

with my models successfully created I can now work on my views.

Views

I have two separate folders where my views live, Coupons folder and the User’s folder. Let’s take a look.

Home: my homepage. It will display an image; it also uses an image from the background to make it look prettier along with a log in or sign-up button!

Coupons viewer folder: within this folder I have three files.

Index: shows the user a list of all available coupons I iterated using each over the coupons to make a nice clean list easier on the eye.

New: the page that the user can submit a new coupon. They must add a store name, description and a coupon code. It will be added to the index of coupons.

Show: will show the details of any coupon you click on. These will show the store its available from, a description of the coupon, and the code to use to get the discount.

User’s folder: in the user’s folder there are four different viewer files.

Edit: the user can edit a previously submitted coupon, maybe there is a new code or its now available at a different store.

Login: this will show the log in page for the user.

Sign up: if there a new user they can create a log in by signing up at this page.

Now I am done! This project was tough but rewarding. I am happy in how everything turned out, my knowledge has expanded. My favorite part of any project is that full circle feeling at the end of it. I can now feel more confident in my knowledge of Sinatra by applying it on something that I created.

--

--