# Let's Build a Web Application in Ruby without Rails

*Published on March 04, 2024*

---


**But...why reinvent the wheel?**

For the past few months, over the weekends and evenings, I've been teaching Ruby and Rails to a few kids in my hometown in India. Part of the reason is to give back, but a big motivation is to show them just how much fun programming can be with Ruby and Rails, in contrast to the standard engineering curriculum that focuses on C++ and Java.

Anyway, although most of them picked up Ruby pretty quickly and even started building simple Rails and Sinatra apps, I found that many struggled with understanding how various components fit together to form a working web application.

For example, here're a few recurring themes of questions I've seen pop up:

- What really happens from the moment you type a URL in the browser until the application loads on the screen?
- How Rails Handles incoming requests from the browser?
- What are Puma and Rack, and how they fit in with a Rails app?
- What is Routing and how to dispatch an HTTP request to the Ruby code (a Rails controller)?
- How is data passed from the controller to the views, and how Rails generates the HTML from the ERB template?
- What's involved in sending the generated HTML response to the browser?
- and many more...

Which is not uncommon, since I struggled with the same questions when I was trying to learn about web application development. But if there's one thing that I've learned about learning itself is that&nbsp;**to understand the benefits of any tool, you should try to accomplish the same task without using that tool.**

Ruby on Rails is not different.

It's a tool that helps us quickly build web applications. And I think we'll have a much better understanding and appreciation of all the things Rails does for us by trying to build a simple yet complete web application without it, trying to implement many of its features ourselves.

This technique has really helped my students, and it also helped me improve my knowledge of Rails. I think it will help you, too.

So, in this series of articles, we will build a web application using only Ruby, without an ounce of Rails. I hope it will give you a peek under the hood and explain some of the core concepts like routing requests, passing data from controllers to views, working with a database, processing forms, and much more.

**In this first post, we will set up our project and create a very simple Ruby application that says 'Hello World' in the browser.**

Here's the list of topics we'll cover in this post.

1. Setup a Project Directory&nbsp;with Bundler.
2. Install Puma Application Server&nbsp;to listen for incoming HTTP requests.
3. Use the Rack Gem&nbsp;to provide the glue between application servers and our application.
4. Create a Web Application&nbsp;that follows the Rack specification.
5. Make Puma Talk to Our Application with a Rackup File
6. Run the Application
7. Use the Rack Reloader Middleware to Reload Application&nbsp;whenever the code changes.

> It goes without saying that this series of posts is only for learning purposes, to understand the fundamentals of web applications and frameworks.&nbsp;**You should absolutely use Rails for your real-world applications.**

Here's a [video](https://www.youtube.com/watch?v=ru-EPTJ3iag) version of this post. Enjoy!

* * *

Let's get started by creating a project directory for our no-rails app!

## Setup a Project Directory

Let's create a brand-new directory named&nbsp;`weby`&nbsp;(yes, it stands for web + ruby) and switch into it.

```bash
➜  mkdir weby
➜  cd weby
```

Next, we'll install bundler, a package (gem) manager for Ruby.

> 💡[Bundler](https://bundler.io/)&nbsp;makes sure Ruby applications run the same code on every machine. It does this by managing the gems that the application depends on. Given a list of gems, it can automatically download and install those gems, as well as any other gems needed by the gems that are listed.

Initialize Bundler for your project by running the following commands from the project root directory.

```bash
➜  gem install bundler
➜  bundle init
```

It creates a&nbsp;`Gemfile`&nbsp;in the project. Whenever we want to use a gem in our project, we will add it to this file, and run&nbsp;`bundle install`&nbsp;command to install it.

As a shortcut, bundler provides the&nbsp;`bundle add`&nbsp;command, which adds the gem to the&nbsp;`Gemfile`&nbsp;and runs the&nbsp;`bundle install`&nbsp;for us.

The first gem we will install using Bundler is Puma, which is an application server.

## Install Puma Application Server

To run our web application, we need an application server. Rails uses&nbsp;[`Puma`](https://puma.io/), and we will use the same.

![puma-logo-large.png](https://writesoftwarewell.com/media/u2wppqsy2zdpto98i0uwlqmru6ri.png "Puma")

Let's install Puma using bundler.

```bash
➜ bundle add puma
```

An application server is different from a web server such as&nbsp;[Apache](https://httpd.apache.org/),&nbsp;[Nginx](http://nginx.org/)&nbsp;or&nbsp;[Caddy](https://caddyserver.com/)&nbsp;which handles incoming requests from the Internet and passes them to the application server.

The purpose of an application server is to take an incoming HTTP request from the web server and pass it to our application. Our application processes the request, builds the response HTML, and hands it back to the application server.

![http-request.png](https://writesoftwarewell.com/media/unbwquzcl4757hf3knjif22l6pve.png "HTTP Request Response Cycle")

After installing Puma on your machine, the next question is:&nbsp;**how will Puma talk to our web application?**

For that, we need Rack.

## Install the Rack Gem

All Ruby application servers and web applications follow the Rack specification. Let's install the Rack gem using Bundler.

```bash
➜  bundle add rack
```

**What is Rack?**

Rack is a very important topic if you want to understand how Ruby-based web frameworks and application servers communicate with each other. To learn more about Rack specification as well as the gem, I suggest you read this article: [The Definitive Guide to Rack for Rails Developers](/definitive-guide-to-rack)

**Here's the gist:**

Rack provides a simple interface using which application servers communicate with the applications.

The Rack specification has two conditions:

- The application should have a&nbsp;`call`&nbsp;method that takes an&nbsp;`env`&nbsp;object representing the incoming HTTP request.
- The application should return an array containing the status, headers, and response.

So that is Rack, a specification. However, in addition to being an abstract protocol, Rack is also a&nbsp;[gem](https://rubygems.org/gems/rack). It provides a bunch of useful tools, such as middleware and a convenient DSL (domain-specific language) to run rack-compliant web applications.

Now, let's create an application Puma will talk to using the Rack specification.

## Create a Web Application

Create the following Ruby script called&nbsp;`app.rb`&nbsp;in the&nbsp;`weby`&nbsp;directory. This is our application.

Yes,&nbsp;**IT IS**&nbsp;our entire app.

```ruby
# weby/app.rb

class App
  def call(env)
    headers = {
      'Content-Type' => 'text/html'
    }
    
    response = ['<h1>Hello World!</h1>']
    
    [200, headers, response]
  end
end
```

As you can see, it follows the Rack protocol.

1. It has a&nbsp;`call`&nbsp;method that takes an&nbsp;`env`&nbsp;object.
2. It returns an array containing the status, headers, and response.

## Make Puma Talk to Our Application with a Rackup File

At this point, we have the Puma application server, and we also have our application&nbsp;`app.rb`.

The only thing missing is the glue that connects the two.

**How does Puma pass the incoming request to our application?**

The answer is a&nbsp;`rackup`&nbsp;file, which is just a Ruby file with&nbsp;`.ru`&nbsp;extension. All Rails apps use this file. When you create a new Rails app, it's generated for you.

When we launch Puma (next step) without any arguments, it looks for this&nbsp;`rackup`&nbsp;file called&nbsp;`config.ru`&nbsp;in the current directory. Check out the&nbsp;[Puma documentation](https://github.com/puma/puma#quick-start)&nbsp;to learn more.

So let's add a&nbsp;`config.ru`&nbsp;file in the&nbsp;`weby`&nbsp;directory.

```bash
➜  touch config.ru
```

Add the following code to this file. It loads the rack gem and also loads our application.

```ruby
require 'rack'
require_relative './app'

run App.new
```

The&nbsp;`run`&nbsp;method is provided by the Rack gem that we installed earlier. It takes a Ruby object as an argument and invokes the&nbsp;**call**&nbsp;method on it.

Since the&nbsp;`App`&nbsp;class has a&nbsp;`call`&nbsp;method, Rack can run it just fine.

At this stage, you should have the following structure in your project directory.

![explorer.png](https://writesoftwarewell.com/media/9r0vun101aytrdk36c64ur0rd2z7.png "Explorer")

We have the final piece of the puzzle. The only thing remaining is running the application!

## Run the Application

To launch the web server, run the&nbsp;`puma`&nbsp;command from the&nbsp;`weby`&nbsp;directory. Alternatively, you can also run&nbsp;`rackup`&nbsp;and it will work the same.

> The benefit of the&nbsp;`rackup`&nbsp;command is that it's app-server agnostic. So it will work even if you're using a different app-server like&nbsp;[Thin](https://github.com/macournoyer/thin)&nbsp;instead of Puma.

```bash
➜  puma
Puma starting in single mode...
* Puma version: 6.0.0 (ruby 3.1.0-p0) ("Sunflower")
*  Min threads: 0
*  Max threads: 5
*  Environment: development
*          PID: 6041
* Listening on http://0.0.0.0:9292
Use Ctrl-C to stop
```

Now our web server is running and serving our application. Open your browser and go to&nbsp;[http://0.0.0.0:9292](http://0.0.0.0:9292/)&nbsp;URL.

![hello-world.png](https://writesoftwarewell.com/media/xhuj2ckryoto5hw8a2slnw9h1y6l.png "Hello World")

Congratulations! You have the simplest web application written in Ruby.

To summarize what we've done so far,

1. We installed an application server that accepts HTTP request and forwards it to our application
2. We created the glue (the&nbsp;`rackup`&nbsp;file) between the application server and our application, so Puma knows where to send the incoming request.
3. We wrote an entire web application in a single Ruby script&nbsp;`app.rb`&nbsp;that takes the incoming HTTP request in the&nbsp;`env`&nbsp;parameter and returns the response containing the status, headers, and the response body.

The cool thing is,&nbsp;**this is exactly what Rails does behind the scenes!**

If you don't believe me, open any Rails app and notice the&nbsp;`config.ru`&nbsp;file. It runs the&nbsp;`Rails.application`&nbsp;as your application.

```ruby
require_relative "config/environment"

run Rails.application
Rails.application.load_server
```

The Rails application class inherits from the Engine class. It contains the entry point of your application. Open the&nbsp;[`engine.rb`](https://github.com/rails/rails/blob/main/railties/lib/rails/engine.rb#L527)&nbsp;file and see for yourself.

```ruby
def call(env)
  req = build_request env
  app.call req.env
end
```

We'll trace the whole path of a Rails request in a separate blog post, but let's fix one minor annoyance we have in our application.

## Use the Rack Reloader Middleware to Reload Application

At this point, we have a small problem.

If you make a change in your application, it won't be reflected in the browser. For this, you need to restart the server by pressing&nbsp;`ctrl + c`&nbsp;on the keyboard.

After making a change, restart the server by running&nbsp;`rackup`&nbsp;or&nbsp;`puma`&nbsp;command again. Your change will show up now.

It can get tedious to restart the server after every change.

**Is there a better way?**

Yes! The Rack gem ships with a&nbsp;[`Rack::Reloader`](https://github.com/rack/rack/blob/main/lib/rack/reloader.rb)&nbsp;middleware that reloads the application after changing the source code.

> A middleware is a small, focused, and reusable application that provides useful functionality to your main app.  
>   
> Middleware sits between the user and the application code. When an HTTP request comes in, the middleware can intercept, examine, and modify it. Similarly, it can examine and modify the HTTP response before forwarding it to the user.

Check out my article on&nbsp;[Rack middleware](/rails-middleware-what-why-and-how)&nbsp;to learn more: [Middleware in Rails: What It Is, How It Works, and Examples](/rails-middleware-what-why-and-how)

### Use the Reloader Middleware

Add the&nbsp;`Rack::Reloader`&nbsp;middleware in the&nbsp;`config.ru`&nbsp;file.

```ruby
require 'rack'
require_relative './app'

use Rack::Reloader, 0 # <- Add this line
run App.new
```

Now launch the web server using&nbsp;`puma`&nbsp;command. The Reloader middleware will automatically reload the application when the next request arrives, if the source code was changed. So we don't have to restart the server manually.

Problem solved.

## What's Next?

Currently, all requests to our app are returning the same response, which is not ideal. In the next post, we will build a routing mechanism to dispatch incoming HTTP requests to separate code blocks.

In future articles in this series, we will explore the following topics.

- Routing requests based on URL patterns
- Project structure and organization
- HTML templates with ERB
- How to serve static files
- Handle errors and logging
- Connect to the database to store and fetch data
- Generate dynamic response HTML
- Adding middleware to handle specific tasks like authentication
- Process form inputs
- and much more...

If those sound interesting to you, consider subscribing to the blog.

Trust me, it's going to be a lot of fun, so stay tuned!!

