方法
- #
- A
- C
- H
- I
- M
Attributes
| [W] | _helpers |
实例公共方法
_helpers_for_modification() 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/abstract_controller/helpers.rb, line 218 def _helpers_for_modification unless @_helpers self._helpers = define_helpers_module(self, superclass._helpers) end _helpers end
all_helpers_from_path(path) 链接
返回给定路径下的助手名称列表。
ActionController::Base.all_helpers_from_path 'app/helpers' # => ["application", "chart", "rubygems"]
来源: 在 GitHub 上
# File actionpack/lib/abstract_controller/helpers.rb, line 93
clear_helpers() 链接
清除此类中所有现有的助手,只保留与此类同名的助手。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/abstract_controller/helpers.rb, line 209 def clear_helpers inherited_helper_methods = _helper_methods self._helpers = Module.new self._helper_methods = Array.new inherited_helper_methods.each { |meth| helper_method meth } default_helper_module! unless anonymous? end
helper(*args, &block) 链接
在模板类中包含给定的模块。
模块可以通过不同的方式指定。以下所有调用都包含 FooHelper
# Module, recommended. helper FooHelper # String/symbol without the "helper" suffix, camel or snake case. helper "Foo" helper :Foo helper "foo" helper :foo
最后两个假设 "foo".camelize 返回 “Foo”。
当传递字符串或符号时,该方法使用 String#constantize 查找实际的模块对象。因此,如果模块尚未加载,它必须是可自动加载的,这通常是这种情况。
支持命名空间。以下调用包含 Foo::BarHelper
# Module, recommended. helper Foo::BarHelper # String/symbol without the "helper" suffix, camel or snake case. helper "Foo::Bar" helper :"Foo::Bar" helper "foo/bar" helper :"foo/bar"
最后两个假设 "foo/bar".camelize 返回 “Foo::Bar”。
该方法也接受一个块。如果存在,该块将在控制器助手模块的上下文中进行评估。此简单调用使 wadus 方法在封闭控制器的模板中可用。
helper do def wadus "wadus" end end
此外,以上所有样式都可以混合使用。
helper FooHelper, "woo", "bar/baz" do def wadus "wadus" end end
来源: 显示 | 在 GitHub 上
# File actionpack/lib/abstract_controller/helpers.rb, line 198 def helper(*args, &block) modules_for_helpers(args).each do |mod| next if _helpers.include?(mod) _helpers_for_modification.include(mod) end _helpers_for_modification.module_eval(&block) if block_given? end
helper_method(*methods) 链接
将控制器方法声明为助手。例如,以下使 current_user 和 logged_in? 控制器方法在视图中可用。
class ApplicationController < ActionController::Base helper_method :current_user, :logged_in? private def current_user @current_user ||= User.find_by(id: session[:user]) end def logged_in? current_user != nil end end
在视图中
<% if logged_in? -%>Welcome, <%= current_user.name %><% end -%>
参数¶ ↑
-
method[, method]- 要在视图中公开的控制器上的一个或多个方法名称。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/abstract_controller/helpers.rb, line 128 def helper_method(*methods) methods.flatten! self._helper_methods += methods location = caller_locations(1, 1).first file, line = location.path, location.lineno methods.each do |method| # def current_user(...) # controller.send(:'current_user', ...) # end _helpers_for_modification.class_eval <<~ruby_eval.lines.map(&:strip).join(";"), file, line def #{method}(...) controller.send(:'#{method}', ...) end ruby_eval end end
inherited(klass) 链接
当继承一个类时,将其助手模块包装在一个新模块中。这确保了父类的模块可以独立于子类进行更改。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/abstract_controller/helpers.rb, line 68 def inherited(klass) # Inherited from parent by default klass._helpers = nil klass.class_eval { default_helper_module! } unless klass.anonymous? super end
modules_for_helpers(modules_or_helper_prefixes) 链接
给定一个类似于 helper 方法接受的值数组,此方法返回一个包含相应模块的数组,顺序相同。
ActionController::Base.modules_for_helpers(["application", "chart", "rubygems"]) # => [ApplicationHelper, ChartHelper, RubygemsHelper]
来源: 在 GitHub 上
# File actionpack/lib/abstract_controller/helpers.rb, line 81