Nat*_*ith 89 ruby ruby-on-rails helpers
为什么我可以在视图中为不同的控制器访问一个控制器的辅助方法?有没有办法在没有黑客攻击/修补Rails的情况下禁用它?
Cra*_*ker 105
@George Schreiber的方法不适用于Rails 3.1; 代码发生了重大变化.
但是,现在有一种更好的方法可以在Rails 3.1中禁用此功能(希望稍后).在config/application.rb中,添加以下行:
config.action_controller.include_all_helpers = false
Run Code Online (Sandbox Code Playgroud)
这将阻止ApplicationController加载所有帮助程序.
(对于任何感兴趣的人,这是创建功能的拉取请求.)
Sim*_*tti 98
答案取决于Rails版本.
将include_all_helpers配置更改为false要应用配置的任何环境.如果要将配置应用于所有环境,请将其更改为application.rb.
config.action_controller.include_all_helpers = false
Run Code Online (Sandbox Code Playgroud)
如果为false,则会跳过包含.
从中删除以下行 ApplicationController
helper :all
Run Code Online (Sandbox Code Playgroud)
这样每个控制器都会加载自己的帮助器.
小智 27
在Rails 3中,actioncontroller/base.rb(在224行左右):
def self.inherited(klass)
super
klass.helper :all if klass.superclass == ActionController::Base
end
Run Code Online (Sandbox Code Playgroud)
所以是的,如果你派出你的班级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.
Run Code Online (Sandbox Code Playgroud)
例如:
class ApplicationController < ActionController::Base
clear_helpers
...
end
Run Code Online (Sandbox Code Playgroud)
实际上在Rails 2中,ActionController :: Base的默认功能是包含所有帮助程序.
变更集6222于02/24/07 20:33:47(3年前)由dhh:默认假设您希望所有帮助者(是的,是的)
更改:
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
end
Run Code Online (Sandbox Code Playgroud)
从Rails 3 beta 1开始,不再是CHANGELOG中提到的情况:
| 归档时间: |
|
| 查看次数: |
36629 次 |
| 最近记录: |