Contact Form

Implementing the “Rails Way”

So far, we’re following the “page paradigm.” Our Contact page hosts the
form. Clicking the submit button makes a request to another page that is
actually a program that returns HTML. Until the late 1990s, this is how
the web worked. But soon after the introduction of CGI, developers began
exploring the possibility of running a single program (an application
server) that responds to any URL, parsing the URL to establish routing,
and generating pages dynamically. This was the genesis of the “web
application paradigm.” It’s how Rails works.

The web application paradigm frees us from one-to-one correspondence of
a URL with a single file or script. It allows us to refactor our code
into object-oriented classes and methods that can be inherited rather
than duplicated, which means we don’t repeat the same code on every page
that processes a form.

The web application paradigm makes it possible to use the
model-view-controller architecture. Instead of looking at the web as
URLs that return pages, we see requests that are routed to controllers
that render views. We can segregate any code that manipulates data into
a model class, instead of mixing HTML with data manipulation in a single
script. With the “web application paradigm,” we can have a generic model
class that isolates the code that connects to a database or validates
form data. We can create models that inherit the generic behavior from a
parent class and get a database connection or validation “for free.”
Unlike the “page paradigm,” we’ll avoid duplicating validation code
every time we need to process a form.