为什么所有的视图都可以使用Rails帮助器? 有没有办法来禁用这个?
为什么我可以在不同控制器的视图中访问一个控制器的帮助器方法? 有没有办法禁用黑客/修补Rails?
@George Schreiber的方法不像Rails 3.1那样工作; 代码已经发生了很大变化。
但是,现在有一个更好的方法来在Rails 3.1中禁用这个function(希望稍后)。 在你的config / application.rb中,添加这一行:
config.action_controller.include_all_helpers = false 这将阻止ApplicationController加载所有的助手 。
(对于任何有兴趣的人, 这里是创buildfunction的拉取请求 。)
答案取决于Rails版本。
Rails> = 3.1
 在要应用configuration的任何环境中,将include_all_helpersconfiguration更改为false 。 如果你想configuration适用于所有的环境,改变它在application.rb 。 
 config.action_controller.include_all_helpers = false 
如果是错误的,它将跳过列入 。
Rails <3.1
 从ApplicationController删除以下行 
 helper :all 
这样每个控制器将加载它自己的助手。
 在Rails 3中, actioncontroller/base.rb (在第224行): 
 def self.inherited(klass) super klass.helper :all if klass.superclass == ActionController::Base end 
 所以是的,如果你从ActionController::Base派生你的类,所有的助手都将被包含在内。 
 为了解决这个问题,请在控制器代码的开头调用clear_helpers ( AbstractClass::Helpers ;包含在ActionController::Base )。  clear_helpers的源代码注释: 
 # Clears up all existing helpers in this class, only keeping the helper # with the same name as this class. 
例如:
 class ApplicationController < ActionController::Base clear_helpers ... end 
实际上,在Rails 2中 ,ActionController :: Base的默认function是包含所有的帮助器。
变化集6222在02/24/07 20:33:47(3年前)通过dhh :默认假设你想要所有的助手,所有的时间(是啊,是啊)
更改:
 class ApplicationController < ActionController::Base helper :all # include all helpers, all the time end 
从Rails 3 beta 1开始,在CHANGELOG中已经不再是这种情况了:
- 补充说,ActionController :: Base现在做的帮手:所有而不是依靠在Rails的默认ApplicationController来做到这一点[DHH]