Sinatra Project

Rupinder Kaur
4 min readMay 30, 2021

Here I am writing a blog for my second project for Flatiron School. I was required to build a Sinatra project MVC(Model View Controller).I built an app for workouts so users have access to their exercises. They can create, read, edit or delete a exercises on the app. This project is so complicated but authors of Sinatra , Active Record and lots of gem helps a lot to make this project so easier to do.

Setting up structure for Project

I installed the corneal gem that a former Flatiron School Student built as a Sinatra Application generator, corneal provides all the necessary documentation to start building a simple Sinatra web application.

  • Install the gem with gem install corneal
  • Generate your app with corneal new APP-NAME
  • Run bundle

With the corneal gem, you can run commands to generate a model or scaffold the entire MVC structure including a migration file.

  • Generate a model: corneal model NAME
  • Generate a controller: corneal controller NAME

With the help of corneal gem , i created my migration tables for users , workouts and exercises. I created my migrations files inheriting from Active Record to manipulate the database.

As project assignment, i had to include at least has_many & belongs to relationship to models. I was able to build the relationship (users has many workouts and exercises, workouts has many exercises , exercises belongs to workout) by inheriting from Active record::Base. Each users have username, password-digest. Each exercises has a name , reps , sets , minutes and foreign key column for the workout_id because exercise belongs to workouts. Each workouts has name and foreign key column of user_id because workouts belongs to users. Then i ran my migrations file by Rack gem(rake db::migrate) to run my tasks and seeded the database.

Build controllers and views:-

When you think about all the CRUD actions, we need a lot of controllers actions for these models. I created controllers that will be inherits from Sinatra::Base. This inheritance transforms our plain old ruby class into a web application by giving it a rack-compatible interface through inheriting from the base of the Sinatra framework. Controllers will define every actions and send a response to a end user. Application controllers inherits from the Sinatra::Base middleware framework is where i enable sessions and session_secret to encrypt user’s password. Users Controllers inherits from the Application controllers routes to sign in and sign up for new users. Exercise controllers inherits from the Application controllers defines routes to handle request and responses relevant to the exercise. This controllers allow users to create exercise , edit and delete the exercise . Workout controllers inherits from the application controllers defines routes to handle and response to create new workouts, edit or delete the workouts.

As a define new RESTful route in my controller, i will create a corresponding view(erb file) . I know , i would have an initial landing page, where a user would do when users is not logged in(welcome.erb).I also created layout.erb template, to style my web application , it also manages the outline frame of site or application. Sinatra uses a templating engine called ERB. Our view file will use extension .erb and can write HTML code there just like in a plain old .html file. Sinatra is also configured by default to look for our .erb files in our views directory. In order to connect the form to my application, i needed to give explicitly directions for routes to where and how to send the data from the user. Views are what is displayed on the web page .Views are then grouped within sub folders, as i implement CREATE, READ,UPDATE,DELETE operations on the database, corresponding views(pages) are created.

As a user should be able to:

  • Sign up or Sign in the app
  • Able to create exercises in your workout folder because workout has many exercises.
  • Update and delete existing enrollments request
  • User is able to view all workouts.

Sinatra::Flash Gem:

Sinatra::Flash is also a good ruby gem. This gem acts like a hash, including iterations and merging. It basically displays validation failures to users with error messages. Like if user puts a wrong password or username , this gem helps to display an error messages as “Wrong password, please try again”,i users didn’t fill username and password, then error message will pop out as “Password can’t be blank and Username can’t be blank”. I implemented those messages in my login and signup routes in my users controllers. I also have to render those messages in my views folder in order to display.

The project taught me so much about how MVC model evalutes each routes in sequence. Routes contains URL pattern and handler information. A handler can be a controller in the MVC application that proccess the request. After i finish with my all code , it was time to style my app with CSS. I got help through google to look around to style my website with many style elements.

Thank you for reading this blog.

--

--