What are helpers in Rails?
A helper is a method that is (mostly) used in your Rails views to share reusable code. Rails comes with a set of built-in helper methods.
One of these built-in helpers is
Here’s an example:
# "less than a minute"
time_ago_in_words(Time.now + 60)
# "1 minute"
time_ago_in_words(Time.now + 600)
# "10 minutes"
This method is helpful whenever you want to display time in this specific format.
Another Rails view helper is
# "10 Thousand"
This is great when you want to take a number & print it as you would read it, which makes it feel more human.
You can find more helpers in the Ruby on Rails documentation.
But did you know you can write your own?
Writing Your Own Helper Methods
If you’re looking to write custom helper methods, the correct directory path is
You write your helpers inside a helper module.
Every Rails application comes with a base helper module by default, it’s called
Here’s where you can add your helper methods.
These methods become available to all your views automatically. Later you’ll learn how to use them in controllers & why that may be a bad idea.
You could write all your helpers in
But there is another option…
You can create helper modules so you can better organize your methods.
- Create a new file under
- Name it something like
- Add a new module which matches the file name
if user.gender == "M"
This code can be used to address a person in a formal way based on their gender.
The main benefit?
You don’t have to repeat this logic in other views when you need it & when you need to change the code… it only has to change in one place.
Using Your New Helper Module
You can use your helper methods in your views.
<%= format_name(@user) %>
If you want to use helpers outside of views you’ll need something else.
How to Use Helpers From Controllers
It’s possible, although not very common, to use helper methods from controller actions.
Before Rails 5, you had to include the helper module.
In newer versions, you can use helpers in your controller with the
helpers (plural) object.
This way you can use helpers from your controller. But think twice before doing this because it may be a design issue.
Consider using a plain Ruby object instead.
Fun With The Rails Console
I love using the Rails console (irb with your Rails app loaded) to try out methods & play around with things.
You can use helpers from the console with
Notice the singular form of “helper” so you don’t get an error message. And remember that the console doesn’t reload code changes automatically.
Best Practices for Writing Rails View Helpers
When should you create a helper method?
Whenever you have logic that produces bits of HTML.
Usually, this falls into one of two categories, one is string formatting & the other is conditional page elements.
If you want to write good helpers don’t use any instance variables, they may be available in your current view, but they may not be in another view.
That will result in an error because of missing variables.
Use parameters, so any data your method needs is clear & explicit.
# wrong way
# do this instead
My final tip is to divide your helpers into modules, where each module name describes clearly what kind of methods it contains.
This won’t help with duplicated method names, which can result in errors & confusion.
All your helpers should have unique names.
As an alternative, try using presenter objects, as explained in this guide.
You have learned about helpers in Rails! A set of utility methods you can use for formatting & handling complex logic in your views.
Now it’s time to create your own helpers.
Thanks for reading 🙂
Really useful article. Thank you for sharing your knowledge. 🙂