DEV Community

Cover image for 49 Days of Ruby: Day 41 -- Web Frameworks: Hanami
Ben Greenberg
Ben Greenberg

Posted on

49 Days of Ruby: Day 41 -- Web Frameworks: Hanami

Welcome to day 41 of the 49 Days of Ruby! 🎉

Yesterday, we began our several-day journey taking brief looks at some of the popular Ruby web frameworks. We started by examining Sinatra, and today, we are going to explore Hanami.

Hanami is a fully-featured web application. It is built on the MVC approach, which stands for: Models, Views and Controllers. When we look at Ruby on Rails tomorrow, we will see another example of MVC.

What is MVC?

The MVC approach is a method to organizing the work of your web application into its distinct categories. It gives you the chance to both mentally organize the application, which can be helpful as it grows in complexity, and also literally organize where the growing amount of files will go.

Let's break it down.

A model is a representation of data objects in your application. It controls the logic, and rules for managing that data. For example, if you have a user table in your database, you would want a User model to manage that data object.

A view is the presentation layer of your application. It is what visitors interact with.

A controller manages the flow between inputs, for example, user input, and models and views.

MVC is an incredibly prevalent form of web development design. You will find the pattern repeated across programming language communities.

How does Hanami do MVC?

MVC in Hanami

Hanami strives to have each part of the code keep to doing only its part as much as possible. Each class, and by extension each file, should stick to a single responsibility.

The following examples come directly from the Hanami Getting Started Guide.

Models in Hanami will live inside the lib folder of your app, just like how the main part of your code in a Ruby gem would live inside lib. Here is an example with a model called Bookshelf:

lib
├── bookshelf
│   ├── entities
│   ├── mailers
│   │   └── templates
│   └── repositories
└── bookshelf.rb
Enter fullscreen mode Exit fullscreen mode

The entities folder is where the model code would go, and the repositories folder is where the logic in interacting with the actual database table would go.

Views in Hanami live inside an apps folder. The default subfolder is called web, but you could have any number of distinct web view structures under different sub-folders.

This is an example file structure for the apps/web folder:

apps/web
├── application.rb
├── assets
│   ├── favicon.ico
│   ├── images
│   ├── javascripts
│   └── stylesheets
├── config
│   └── routes.rb
├── controllers
├── templates
│   └── application.html.erb
└── views
    └── application_layout.rb
Enter fullscreen mode Exit fullscreen mode

As you see above, there is an application.rb file where all the settings for the app go. The controllers go inside the controllers folder, and the views go inside the views folder. There is also a config/routes.rb to delineate the different web routes a user might access the app with, and which controller action they correspond to.

Tomorrow, we will see how MVC looks like with Ruby on Rails, and you will notice how different the structure is.

For the rest of today, try to build your first Hanami web app by following along with the Get Started Guide!

Come back tomorrow for the next installment of 49 Days of Ruby! You can join the conversation on Twitter with the hashtag #49daysofruby.

Top comments (0)