We are going to implement a controller structure similar to Rails. By default, Rails stores the controller classes in the controllers directory. We'll do the same.

Here's an example controller class that returns a string.

Note: Don't write any code yet, I am only showing what we'll build throughout the lesson

# controllers/articles_controller.rb

class ArticlesController < ApplicationController

  # GET /articles/index
  def index
    'all articles'
  end
end

It's not exactly similar to a Rails controller, which stores the data in the instance variables. However, it's good enough to keep it simple and explain the fundamentals of controllers. We'll make it more Rails-like later, once we introduce the concept of a model.

After creating a controller class and the action method, you can define a route to the controller action as follows:

get 'articles/index'

Whenever the user navigates to the articles/index URL, our application will call the index method on the ArticlesController class. It will also pass the HTTP request environment, i.e. the env hash which will be accessible in the index action.

Just like Rails.

Later, we'll use the Rails conventions, so we don't have to use index in the URL, inferring it by default.

Let's get started. We'll accomplish this in three simple steps.