Learning Goals 🎯

  1. Understand how Rails handles requests and translates them into actions
  2. Understand how an action in a Rails controller sends back text or HTML to the browser
  3. Be able to use embedded Ruby to create variables in the controller that can be used in a Rails template.

Prerequisites 🤔

What is a Web Server?

Now that we've discussed how our browser communicates with websites through requests, it's time to discuss what it means to build web servers ourselves! In short, a web server is a program that is set up to receive requests and send back responses. Rails is a framework built on top of Ruby (a collection of code written in Ruby that provides us tools to serve a specific function, in this case: a web server!) that has ways for us to exactly this! Let's dive in.

Routes

In last week's lecture, we were sending requests. Now, we're writing a program to receive them. On a high-level view of things, when we use a framework that deals with the heavy networking stuff for us, this means mapping a literal route (like /users/sign_in) to a function that gets run and responds with the text to be rendered.

To understand this idea, let's take a look at how this might look at in a few light-weight web server frameworks in different languages. The reason we're using an example that isn't Rails is because these languages are not opinionated and thus are much more direct with how a route becomes a function. We'll talk about what this means in a little bit.