- #
- A
- C
- H
- I
- M
属性
[W] | _helpers |
实例公共方法
_helpers_for_modification() 链接
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 107
clear_helpers() 链接
清除此类中的所有现有帮助程序,仅保留与该类同名的帮助程序。
# File actionpack/lib/abstract_controller/helpers.rb, line 222 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 211 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 140 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(*args, &block) # controller.send(:'current_user', *args, &block) # end _helpers_for_modification.class_eval <<~ruby_eval.lines.map(&:strip).join(";"), file, line def #{method}(*args, &block) controller.send(:'#{method}', *args, &block) end ruby2_keywords(:'#{method}') ruby_eval end end
inherited(klass) 链接
当继承一个类时,将其帮助器模块包装在一个新模块中。这确保父类的模块可以独立于子类的模块进行更改。
来源:显示 | 在 GitHub 上
# File actionpack/lib/abstract_controller/helpers.rb, line 84 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
接受的值,此方法返回一个数组,其中包含相应的模块,顺序相同。
来源:在 GitHub 上
# File actionpack/lib/abstract_controller/helpers.rb, line 97