Let's require the erb gem and add a new method called render on the ApplicationController class. We're adding it to the base controller class so that all controller classes will inherit it.

require 'erb'

class ApplicationController
  attr_reader :env

  def initialize(env)
    @env = env
  end

  # new method
  def render(view_template)
    erb_template = ERB.new File.read(view_template)
    erb_template.result(binding)
  end
end

The new code does the same thing that we saw in our simplified example earlier. However, there're three important things to note here:

  1. It reads the string template from a file called view_template, which we'll learn more about later.

  2. Then it calls the binding method to get the binding in the context of the controller class, making all the instance variables available to the binding.

  3. Finally, it renders the response by calling the result method on the ERB template and passing the binding.

If this sounds confusing, don't worry. I promise everything will start making sense very soon, especially once you see how we use the render method!