跳至内容 跳至搜索
方法
H
M

实例公共方法

helper_attr(*attrs)

声明控制器属性的助手访问器。例如,以下代码向控制器添加了新的 namename= 实例方法,并使其可用于视图:attr_accessor :name helper_attr :name

参数

  • attrs - 将转换为助手方法的属性名称。

# File actionpack/lib/action_controller/metal/helpers.rb, line 84
def helper_attr(*attrs)
  attrs.flatten.each { |attr| helper_method(attr, "#{attr}=") }
end

helpers()

提供一个代理,用于从视图外部访问助手方法。

请注意,该代理在不同的视图上下文中渲染。这可能会导致 `capture` 方法出现不正确的行为。在使用 capture 时,请考虑改用 helper

# File actionpack/lib/action_controller/metal/helpers.rb, line 94
def helpers
  @helper_proxy ||= begin
    proxy = ActionView::Base.empty
    proxy.config = config.inheritable_copy
    proxy.extend(_helpers)
  end
end

modules_for_helpers(args)

重写 modules_for_helpers 以接受 :all 作为参数,该参数会加载 `helpers_path` 中的所有助手。

参数

  • args - 助手列表

返回

  • array - 提供的助手列表的规范化模块列表。

# File actionpack/lib/action_controller/metal/helpers.rb, line 112
def modules_for_helpers(args)
  args += all_application_helpers if args.delete(:all)
  super(args)
end