views:

31

answers:

2

I was able to find some docs in apidoc.com but can't tell what exactly helper does.

helper_method seems more straight forward: to make some or all of the controller's methods available for use to the View. What about helper? Is it to do the other way around: import helper methods in a file or in a Module? (Maybe the name helper and helper_method are kind of alike... instead of share_methods_with_view and import_methods_from_view)

reference: http://apidock.com/rails/ActionController/Helpers/ClassMethods/helper

+1  A: 

helper is "at a higher level" -- it is used to bring in (require) a module into the controller's class.

from the docs:

 helper :foo             # => requires 'foo_helper' and includes FooHelper

AFAIK, the methods in a module required/included by the helper method are NOT provided to the controller's views.

To enable a view to use a controller method, you use the helper_method method

Added: I agree with you that the names can be confusing. Naming is a hard problem. The good news is that Rails gets it right most of the time.

Also, note that the helper method is suggesting a naming standard for modules that contain "helper" methods used by one or more controllers: name the module "foo_helper" rather than something else. This is a good example of a Rails convention.

Larry K
+1  A: 

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

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
Jeremy