ruby-on-rails-4 - Rails 4 中的助手

标签 ruby-on-rails-4 helpers

我用 Rails 编写了一个应用程序,它发展迅速,我正在学习它。但我不了解助手。

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

为什么在任何客户看来都可以在用户助手模块上调用头像助手方法?
那么,为什么要在许多文件中分开助手呢?

提前致谢。

PS:Rails 的第 4 版。

最佳答案

因为默认情况下所有助手都包含在所有 Controller 中。在这种情况下,单独的文件实际上只是用于逻辑分离。您可以更改该行为:

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

关于ruby-on-rails-4 - Rails 4 中的助手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21476140/

相关文章:

ruby-on-rails - 如何从另一个辅助方法中调用 Rails 辅助方法?

ruby-on-rails-4 - rails_admin - 如何在 ListView 中按自定义字段排序

ruby-on-rails-4 - Rails 无法加载 shoulda-matchers 3.1.1 gem,#<RSpec 的未定义方法 `allow_value'

ruby-on-rails - 设计 Google Oauth 可以完美运行,但不会在用户创建时登录,需要额外登录

ruby-on-rails - 在不存在的方法上使用 Rails 表单助手

ruby-on-rails - 我的 rails 助手将 '<' 渲染为 '&lt;' !如何使其正确渲染?

ruby-on-rails - Rubymine 不理解路径助手

ruby-on-rails - 在 ActiveJob 中排队多个作业

mysql - rails Controller Action 悬挂

ruby-on-rails - 如何删除和返回 ActiveRecord 关系对象中的第一个元素?