What do helper and helper_method do?
Asked Answered
M

2

225

helper_method is straightforward: it makes some or all of the controller's methods available to the view.

What is helper? Is it the other way around, i.e., it imports helper methods into a file or a module? (Maybe the name helper and helper_method are alike. They may rather instead be share_methods_with_view and import_methods_from_view)

reference

Meshach answered 21/10, 2010 at 22:55 Comment(0)
R
329

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
Ruthven answered 22/10, 2010 at 1:40 Comment(5)
In Rails >= 3.1, 'helper :all' in the application controller is replaced by the 'include_all_helpers' configuration setting in application.rb. See stackoverflow.com/questions/1179865.Outshoot
@Ruthven I know this post is old, but well said. Helped me!Siddra
There is a difference between using "helper" and "include" beyond some simple syntactical sugar. "helper" makes the named module available to views, and only views. "include" makes the named module available to views and to the controllers. Both impact inherited controllers and their views.Allege
@R_G Did you notice that, the last snippet include UserHelper is in the HomeHelper module, instead of in the controller? AFAIK, simply including the helper module in the controller won't share the methods with views.Durand
@FranklinYu including the helper module in the controller will actually share the methods with views, so R_G is correct.Larkspur
G
0

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 and again.

Here's an example to simplify the above definition:

Here is a code, where you would have something like this in the view:

<% if @user && @user.email.present? %>
  <%= @user.email %>
<% end %>

We can clean it up a little bit and put it into a helper:

module SiteHelper
  def user_email(user)
    user.email if user && user.email.present?
  end
end

And then in the view code, you call the helper method and pass it to the user as an argument.

<%= user_email(@user) %>

This extraction makes the view code easier to read especially if you choose your helper method names wisely.

So I hope this clears things up a little for you.

Source for the quotation Source for the code

Gable answered 26/2, 2022 at 19:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.