A helper method is used to perform a particular repetitive task common across multiple classes. This keeps us from repeating the same piece of code in different classes again n again. Whereas the class-specific methods define its behaviour, the helper methods assist in that process. You can think of Integer.
A helper method is just a method that helps you do something else. For example, if you had to find the square root of a number multiple times within a method, you wouldn't write out the code to find the root each time you needed it, you'd separate it out into a method like Math.
The method helper_method
is to explicitly share some methods defined in the controller to make them available for the view. This is used for any method that you need to access from both controllers and helpers/views (standard helper methods are not available in controllers). e.g. common use case:
#application_controller.rb
def current_user
@current_user ||= User.find_by_id!(session[:user_id])
end
helper_method :current_user
the helper
method on the other hand, is for importing an entire helper to the views provided by the controller (and it's inherited controllers). What this means is doing
# application_controller.rb
helper :all
For Rails > 3.1
# application.rb
config.action_controller.include_all_helpers = true
# This is the default anyway, but worth knowing how to turn it off
makes all helper modules available to all views (at least for all controllers inheriting from application_controller.
# home_controller.rb
helper UserHelper
makes the UserHelper methods available to views for actions of the home controller. This is equivalent to doing:
# HomeHelper
include UserHelper
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With