Sinatra Integrating Models Walkthrough


In previous lessons, we've applied logic to data provided by the user directly in our application controller. While this works, it does not follow the principle of 'separation of concerns' - our files should do one thing and one thing only. In this code-along lesson, we'll learn how to move the logic to a model in a Sinatra application. By doing this, we'll create our first full Model-View-Controller application in Sinatra!

Learning Goals

  • Create a model in Sinatra
  • Link to created models from the application controller
  • Create instances of a model in the application controller
  • Display data from instances of a model in a view


We'll use input from a form to create an instance of a model, and then send that instance back to a view to be displayed to the user. As an example, we're going to create a web application that analyzes a block of text from the user - showing the number of words, most common letters, and least common letters to us.

To code along, fork and clone this lab. Run bundle install to make sure all of your dependencies are installed.

Starter Code

Let's take a closer look at the starter code. Run shotgun to make sure that your application can run.


  • The controller has two routes:
    • get '/' do, which renders the index.erb page.
    • post '/' do, which receives the form data through params and renders the results page.

Creating a model

We could analyze all of the data from params[:user_text] in our application controller, but our route would get messy very quickly. Instead, let's create a new class inside of our models directory that will take care of the analysis of the text. In your models directory, open the file called text_analyzer.rb.

We're not going to go deeply into creating models in this lesson, as you've covered it in depth in our unit on object oriented programming. Instead, paste the following code in to your text_analyzer.rb file:

class TextAnalyzer
  attr_reader :text

  def initialize(text)
    @text = text.downcase

  def count_of_words
    words = text.split(" ")

  def count_of_vowels

  def count_of_consonants

  def most_used_letter
    s1 = text.gsub(/[^a-z]/, '') # gets rid of spaces
    arr = s1.split('')
    arr1 = arr.uniq
    arr2 = {} do |c|
      arr2[c] =  arr.count(c)

    biggest = { arr2.keys.first => arr2.values.first }

    arr2.each do |key, value|
      if value > biggest.values.first
        biggest = {}
        biggest[key] = value


The model above has an initializer which takes in a string text, converts it to lowercase, and saves it to an instance variable @text. This instance variable is then used in the four instance methods, which provide information on the block of text in question. If we wanted to use this class on its own, we could do the following:

my_text ="The rain in Spain stays mainly on the plain.")
my_text.count_of_words #=> 9
my_text.count_of_vowels #=> 13
my_text.count_of_consonants #=> 22
my_text.most_used_letter #=> {"n" => 6}

In general our models are agnostic about the rest of our application - we could drop this class into a Command Line or Ruby on Rails app and it would function in the exact same way.

Using a model in the controller

To use the model we've created with our controller, we need to connect the two. In order to do this, we'll add a require_relative statement to the controller so that this file can use this new class.. At the top of app.rb, add require_relative "models/text_analyzer.rb". This now gives us the ability to reference the TextAnalyzer class and invoke its new method.

Now, let's take the data from params[:user_text] (in the post '/' do route) and feed it into a new instance of the TextAnalyzer class:

post '/' do
  text_from_user = params[:user_text]

  @analyzed_text =

  erb :results

We can shorten this to:

post '/' do
  @analyzed_text =[:user_text])

  erb :results

We now have the instance of TextAnalyzer saved to an instance variable called @analyzed_text. This means that we can call it and its methods from the results.erb view that is being rendered, using erb tags!

Using instance variables in ERB

In our results.erb file, use erb tags to display the data stored in the @analyzed_text variable. Your end result should look something like this:

Full MVC

Congratulations! You've now created your first Sinatra app that uses a model, views, and a controller! You are taking user input in a form, sending it via params to the 'post' route where a new instance of the model is created using the data from the form. This instance is passed back to the view, where it is rendered using erb tags. Pat yourself on the back, this is a big milestone in your developer journey!

Unlock your future in tech
Learn to code.

Learn about Flatiron School's Mission

With a new take on education that falls somewhere between self-taught prodigy and four-year computer science degree, the Flatiron School promises to turn students with little programming experience into developers.

In the six months since the Manhattan coding school was acquired by WeWork, it has spawned locations in Washington, D.C., Brooklyn, and London. Now, WeWork is opening a fourth Flatiron School location, this time in Houston.

Adam Enbar, Flatiron School's cofounder, believes now is the time to grow. "How the world is changing has impacted working and learning in very similar ways. We think education fundamentally is about one thing: enabling people to pursue a better life."

Learn. Love. Code.
Students come to Flatiron School to change their lives. Join our driven community of career-changers and master the skills you need to become a software engineer or a data scientist.
Find Us