Ruby-on-rails 为什么所有的 Rails 助手总是对所有视图可用?有没有办法禁用它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1179865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why are all Rails helpers available to all views, all the time? Is there a way to disable this?
提问by Nate Smith
Why can I access helper methods for one controller in the views for a different controller? Is there a way to disable this without hacking/patching Rails?
为什么我可以在另一个控制器的视图中访问一个控制器的辅助方法?有没有办法在不破解/修补 Rails 的情况下禁用它?
回答by Craig Walker
@George Schreiber's method doesn't work as of Rails 3.1; the code has changed significantly.
@George Schreiber 的方法在 Rails 3.1 中不起作用;代码发生了重大变化。
However, there's now an even better way to disable this feature in Rails 3.1 (and hopefully later). In your config/application.rb, add this line:
但是,现在有一种更好的方法可以在 Rails 3.1(希望以后)中禁用此功能。在您的 config/application.rb 中,添加以下行:
config.action_controller.include_all_helpers = false
This will prevent ApplicationController from loading all of the helpers.
这将阻止 ApplicationController 加载所有帮助程序。
(For anyone who is interested, here's the pull request where the feature was created.)
(对于任何感兴趣的人,这里是创建该功能的拉取请求。)
回答by Simone Carletti
The answer depends on the Rails version.
答案取决于 Rails 版本。
Rails >= 3.1
导轨 >= 3.1
Change the include_all_helpersconfig to falsein any environment where you want to apply the configuration. If you want the config to apply to all environments, change it in application.rb.
在要应用配置的任何环境中将include_all_helpers配置更改为false。如果您希望配置适用于所有环境,请在application.rb.
config.action_controller.include_all_helpers = false
When false, it will skip the inclusion.
当为 false 时,它将跳过包含。
Rails < 3.1
导轨 < 3.1
Delete the following line from ApplicationController
从以下行中删除 ApplicationController
helper :all
In this way each controller will load its own helpers.
通过这种方式,每个控制器将加载自己的助手。
回答by George Schreiber
In Rails 3, actioncontroller/base.rb(around line 224):
在 Rails 3 中actioncontroller/base.rb(第 224 行左右):
def self.inherited(klass)
super
klass.helper :all if klass.superclass == ActionController::Base
end
So yes, if you derive your class from ActionController::Base, all helpers will be included.
所以是的,如果你从 派生你的类ActionController::Base,所有的助手都将被包括在内。
To come around this, call clear_helpers(AbstractClass::Helpers; included in ActionController::Base) at the beginning of your controller's code. Source code comment for clear_helpers:
要解决此问题,请在控制器代码的开头调用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.
E.g.:
例如:
class ApplicationController < ActionController::Base
clear_helpers
...
end
回答by databyte
Actually in Rails 2, the default functionality of ActionController::Base was to include all helpers.
实际上在Rails 2 中, ActionController::Base 的默认功能是包含所有帮助程序。
Changeset 6222 on 02/24/07 20:33:47 (3 years ago) by dhh: Make it a default assumption that you want all helpers, all the time (yeah, yeah)
Changeset 6222 on 02/24/07 20:33:47 (3 years ago) by dhh:默认假设你一直想要所有的助手(是的,是的)
change:
改变:
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
end
As of Rails 3 beta 1, that is no longer the case as noted in the CHANGELOG:
从 Rails 3 beta 1 开始,情况不再如 CHANGELOG 中所述:
- Added that ActionController::Base now does helper :all instead of relying on the default ApplicationController in Rails to do it [DHH]
- 添加 ActionController::Base 现在做 helper :all 而不是依赖 Rails 中的默认 ApplicationController 来做 [DHH]

