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.

--

--