我用 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
tofalse
.
http://api.rubyonrails.org/classes/ActionController/Helpers.html
关于ruby-on-rails-4 - Rails 4 中的助手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21476140/