Rails — Sometimes we all need a helping hand.

Photo by Toa Heftiba on Unsplash

Programming can be tough. App development can be even tougher. There is so much to think about. The planning stage, the actual making it work stage, and finally refactoring. Thanks to Rails MVC and separation of concerns, it makes it so much easier to keep your code readable for the next developer and even yourself down the road. What I want to highlight in this post is, drumroll, view helper methods!

In case you don’t know what I am talking about, helper methods “help” abstract the Ruby logic out of your view (kind of a pun). Nobody wants too much logic in their view folders.

Your helper folder lives nested in the app folder. If you created your models with rails g controller then you should automatically have a helper module for each corresponding view.

A quick example. Perhaps you want to customize the title of a board, say on Pinterest. You want it to say “My Board” if it is your, and you want it to say “Kenneth’s Board” if someone else is visiting your board. You might find something like this in your view:

We would like to take this if statement away. It doesn’t need to be in our view. So we move its helper module (users_helper.rb) . Like this:

Now we have replaced all that logic with a simple helper method with the @user instance.

The above example is a small example for illustrative purposes, but helper methods can take a lot of clutter out of your view. They should not be taken lightly!

As always, thanks for reading, and happy coding! =)

Originally published at https://uharston.github.io on August 3, 2020.

Full-Stack Software Engineer and Lifelong Learner