문제

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.

도움이 되었습니까?

해결책

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

다른 팁

To add to Mike Campbell's answer:


Framework

As magic as it is, Rails is a set of files which are called sequentially

These files hold classes, methods, etc; but they're still files. And that means that when you run an action through Rails, it loads up a series of other dependent files to help it run (that's what a framework is)

The design of Rails is such that your helper methods are all loaded each time you run an action. I don't know why, but it helps administer the methods for different areas of your app

So to answer your question, there's no real reason why the helpers are split up, at least with Rails 4

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top