The Helper

Nate Nelson
2 min readMar 8, 2021

Sinatra is impressive, as far as impressive can be when compared with a CLI application. Understanding the MVC model helps bring distant elements, html, database, ruby and the web, together into a cohesive and joined element. The models reflect and act on aspects of the database, controllers direct and control data flow on the web and the views create the user experience.

One of the toughest aspects that I did not take full advantage when I was creating my very first web app, was implementing the advantage of helper function. Helper functions are functions that either live in their own class, or append themselves in the application controller. They live within the controller instance and can be called on within the controller and within loaded views.

My web-app was a recreation of a product management system that I made for my previous boss. In it I needed to create a way to add item, and their various item codes to a product. Each item required authentication to ensure that it belonged to the current user, order and product. If anything is amiss their needed to be redirects and flash-messages. For each controller request my code was not looking dry at all.

Then I reviewed helpers and I wish I had sooner. Pretty soon I turned over a hundred lines of code into this:

By implementing this code above with each controller, all I had to do was drop one line of code to verify there is a logged in user, find a correct order and that that user matched the order requested. Item codes could be checked for required uniqueness and length with and without the view form and redirects given if the order is completed.

Looking forward, I will be creating many more helper functions to help making DRYer and more beautiful code.

--

--