Benjamin Cheng
2 min readFeb 25, 2021

MVC based Sinatra Web Application

Sinatra is a framework for creating simple web applications in Ruby. Using a model, view, controller (MVC) style of programming allows for a more simpler way of “writing, reading, and debugging code”. With this type of structure, it is easier to write an more understandable and easier to debug application.

  • Models are the logic of the application, data can be manipulated and saved.
  • Views are the ‘front-end’ of the application. This is where the visuals and text that the user will be interacting with. It generally contains HTML, CSS, and some Ruby.
  • Controllers are the middle man of models and views. Controllers allows the browser to relay data to the application and vice versa.

Structuring the files into a MVC setup is one of the first things that identifies the models, views, and controller environment.

Most of the gems can be installed with running bundle install in terminal to install all missing gems necessary for your application.

Models are generally written as Ruby classes. For our example, the user has an email and a password attribute that can be set. Additionally, classes can be related to other classes using has_many or belongs_to.

Controllers is where the application configurations, routes, and actions can be implemented. It represents the application logic and the flow. Controllers are also written in Ruby and consist of ‘routes’ that take requests sent from the browser (get, post, etc.). The controller will run the code based on the request and then render them into .erb files for the user to see.

Views contains the code that the user will be seeing and interacting with. The view files will generally use Embedded Ruby ( .erb ) and HTML. Ruby logic can also be applied here.

All in all, you get a simple and easy to read architecture of files that lets you develop a web based application. Sinatra is widely used, even by companies like Apple, Heroku, etc.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Benjamin Cheng
Benjamin Cheng

No responses yet

Write a response