Translated from: https://www.rubyguides.com/2020/01/rails-helpers/

What is a Helper in Rails?

A Helper is a function (most of the time) that is used to share reusable code between Rails views. Rails has a built-in set of Helper functions.

One of them is time_ago_in_words.

example

time_ago_in_words(Time.now)
# "less than a minute"

time_ago_in_words(Time.now + 60)
# "1 minute"

time_ago_in_words(Time.now + 600)
# "10 minutes"

This function displays the date in the specified format.

Another Helper function for Rails views is number_to_human.

Example:

number_to_human(10_000)
# "10 Thousand"

This function is useful when you want to display numbers in a readable form.

You can find more in the Rails documentation, content: https://api.rubyonrails.org/

So how do you define your own Helper functions?

Write your own Helper functions

If you’re going to write Helper functions, the right path is app/helpers, and write your Helper functions in its Helper module.

Every Rails application, by default, comes with a basic helper module named ApplicationHelper. This is where you put your helper functions.

Once you write helper functions in the module, those functions will automatically become available in the view. I’ll show you how to use them in Controller and why this is a bad idea later. You can write all the helper functions in ApplicationHelper if you want, but there are better ways to organize them. You can create your own file to store the related helper functions.

steps

  • inapp/helpersIn, create a new file
  • nameduser_helper.rb
  • Add a new module with the same name

The sample

# app/helpers/user_helper.rb
module UserHelper
  def format_name(user)
    if user.gender == "M"
      "Mr. #{user.name}"
    else
      "Ms. #{user.name}"
    end
  end
end

This code can be used where you need to format the user name according to the name.

The upside?

When you need to use this logic, you don’t have to repeat it every time you use it. Also, when you need to change the logic, you only need to change it once.

That’s good.

Use your new Helper module

You can use the Helper function that you just defined in your view.

Like this:

<%= format_name(@user) %>

Simple, right?

If you want to use these Helper functions outside of your view, you have some work to do.

How do I use Helper functions in Controller

It’s possible to do this with a Helper function in a controller, but it’s not very common.

Before Rails 5, you had to introduce Helper modules.

In later versions of Rails 5, you can use it by calling the call to the helpers object in the Controller.

Like this:

class UsersController
  def index
    helpers.time_ago_in_words(Time.now)
  end
end

You can use a Helper function in a Controller in this way, but you have to think carefully about whether you want to do it or not, because it’s probably a design problem.

Consider using a plain Ruby object instead.

In the Rails Console, use the Helper functions

I like to use the Rails terminal (the IRB that loads the Rails app environment) to use functions and other things.

Helper objects are introduced!

You can use helpers in the terminal, using helper.method_name.

Note the singular form of helper, or you will receive an error message. Also, remember that under the terminal, when you modify the code, it does not reload.

Best practices for writing Rails view functions

So, when do you create a Helper function?

When you need to use some kind of inclusion logic in HTML.

In general, there are two types of requirements in this case, one for string processing and one for generating page elements based on conditions.

Other Suggestions

If you want to write best practice helper functions, you should not use instance variables. Instance variables may be useful for one view, but may not be useful for another.

In this case, the error message is returned because the variable is missing.

The solution

Passing the external variables you need to use as parameters will make your Helper function clear and unambiguous.

# wrong way
def eat_healthy
  @fruit.eat
end
# do this instead
def eat_healthy(fruit)
  fruit.eat
end

My final tip is to put your Helper functions in different module files, depending on what they do. Then give these modules a descriptive name so that the code can be seen at a glance.

However……

When writing Helper functions, be careful that the names of the functions are unique. Avoid double names, which can cause conflicting errors.

Another option is to use

Another option is to use Presenter Objects, which is demonstrated here.

conclusion

In this article, you learned a little about Rails Helper functions. A Helper function is a collection of functions that are called in the view. It is used to handle problems such as formatting text and abstracting complex presentation logic.

Now, it’s time to write your own Helper functions.

Thanks for reading.