Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Helpers in Rails 4

I make an application writed in Rails, it's growing fast and I learning with it. But I'm don't understand about helpers.

application_helper.rb

module ApplicationHelper
    # This file it's empty
end

users_helper.rb

module UsersHelper

    def avatar
        # Do something
    end
end

customer_helper.rb

module CustomerHelper
    # This file it's empty
end

Why in any customer's view can call avatar helper method on user helper module? Then, why separate helpers in many files?

Thanks in advance.

P.S: Rails' version 4.

like image 879
Javier Valencia Avatar asked Mar 21 '23 17:03

Javier Valencia


1 Answers

Because all helpers are included in all controllers, by default. The separate files are really just for logical separation in this scenario. You can change that behaviour though:

By default, each controller will include all helpers.

In previous versions of Rails the controller will include a helper whose name matches that of the controller, e.g., MyController will automatically include MyHelper. To return old behavior set config.action_controller.include_all_helpers to false.

http://api.rubyonrails.org/classes/ActionController/Helpers.html

like image 93
Mike Campbell Avatar answered Apr 26 '23 07:04

Mike Campbell