Building a Sinatra Application

veryfine69
3 min readSep 15, 2019

--

I built my Ruby project using the Sinatra framework. Sinatra allows the user to implement CRUD and MVC. I decided to build a basic application called Trekker. Trekker is a trip planner that allows users to create lists and keep track of their destinations. The user can also modify and delete items from their lists.

MVC

MVC stands for model, view, and controller. MVC is the main concept that drives Sinatra to work.

CRUD

CRUD stands for create, read, update, and destroy. A basic CRUD application allows a user to create an object through a form, read and display are based on the input values of a user. Users can update or modify an existing list, or delete data completely. This is the basic concept of all web applications.

Migration Table

The project must start with proper migration tables and data associations. I created two models for my application: User and Trek. The first thing I did was put pencil to pad and created two models and thought about how they would relate to one another. In order, to bring these models to life, we can create the database, so we can create tables needed to bring these models to life. Having a migration table will allow users to enter their information and have that data persist to the database.

The relationships are as follows:

  • A user has_many treks
  • A trek belongs_to a user

In order to protect a user’s password information, I used the bcrypt gem to implement the has_secure password functionality into User objects. I also used validates_presence_of :username , :email because those two values are necessary in order to create an account.

User.rb

Just like the User class, the Trek class has :name and :location that are needed in order to create a trek.

Trek.rb

Controllers

Next, I built the controller actions that connects to the views directory. These actions are designated in routes, so that proper forms and data are being instantiated for the user. Below is an example of rendering a get request to render the index page, which is also the user’s homepage.

treks_controller.rb

Views

Each view file that is rendered through a route is found in the views directory. To ensure proper separation of concerns, each controller has it’s own subdirectory for erb files. Below is an example of the index file that is rendered by the get route above.

index.erb

Conclusion

In conclusion, I hope to deploy my app and update this blog afterwards once it goes live. This has been a wonderful learning experience. I hope to have my app grow as I do as a developer.

--

--