跳至内容 跳至搜索

抽象控制器基类

AbstractController::Base 是一个低级 API。任何人都不应直接使用它,并且子类(如 ActionController::Base)应提供自己的 render 方法,因为渲染根据上下文意味着不同的事物。

方法
A
C
F
I
M
P
R
S
包含的模块

属性

[R] abstract
[R] abstract?

类公共方法

abstract!()

定义一个控制器为抽象。有关更多详细信息,请参阅 internal_methods

# File actionpack/lib/abstract_controller/base.rb, line 57
def abstract!
  @abstract = true
end

action_methods()

应被视为操作的方法名称列表。这包括控制器上的所有公共实例方法,减去任何内部方法(请参阅 internal_methods),添加回任何内部方法,但仍存在于类本身中。

返回

  • Set - 应被视为操作的所有方法的集合。

# File actionpack/lib/abstract_controller/base.rb, line 96
def action_methods
  @action_methods ||= begin
    # All public instance methods of this class, including ancestors
    # except for public instance methods of Base and its ancestors.
    methods = public_instance_methods(true) - internal_methods
    # Be sure to include shadowed public instance methods of this class.
    methods.concat(public_instance_methods(false))
    methods.map!(&:to_s)
    methods.to_set
  end
end

clear_action_methods!()

action_methods 已缓存,有时需要刷新它们。 ::clear_action_methods! 允许您执行此操作,因此下次运行 action_methods 时,它们将被重新计算。

# File actionpack/lib/abstract_controller/base.rb, line 111
def clear_action_methods!
  @action_methods = nil
end

controller_path()

返回完整的控制器名称,带下划线,不带结尾的 Controller。

class MyApp::MyPostsController < AbstractController::Base

end

MyApp::MyPostsController.controller_path # => "my_app/my_posts"

返回

  • 字符串

# File actionpack/lib/abstract_controller/base.rb, line 125
def controller_path
  @controller_path ||= name.delete_suffix("Controller").underscore unless anonymous?
end

internal_methods()

控制器所有内部方法的列表。这将找到控制器的第一个抽象超类,并获取该抽象类上的所有公共实例方法的列表。控制器上的公共实例方法通常被视为动作方法,因此将删除在抽象类上声明的方法。(ActionController::MetalActionController::Base 被定义为抽象)

# File actionpack/lib/abstract_controller/base.rb, line 76
def internal_methods
  controller = self
  methods = []

  until controller.abstract?
    methods += controller.public_instance_methods(false)
    controller = controller.superclass
  end

  controller.public_instance_methods(true) - methods
end

method_added(name)

添加新的动作方法时,刷新缓存的 action_methods

# File actionpack/lib/abstract_controller/base.rb, line 130
def method_added(name)
  super
  clear_action_methods!
end

supports_path?()

如果给定的控制器能够呈现路径,则返回 true。AbstractController::Base 的子类可能返回 false。例如,电子邮件控制器不支持路径,只支持完整的 URL。

# File actionpack/lib/abstract_controller/base.rb, line 199
def self.supports_path?
  true
end

实例公共方法

action_methods()

委托给类的 ::action_methods

# File actionpack/lib/abstract_controller/base.rb, line 170
def action_methods
  self.class.action_methods
end

action_name

返回此控制器正在处理的动作的名称。

# File actionpack/lib/abstract_controller/base.rb, line 42
attr_internal :action_name

available_action?(action_name)

如果动作的方法可用且可以分派,则返回 true,否则返回 false。

请注意,action_methods.include?("foo") 可能返回 false,而 available_action?("foo") 返回 true,因为此方法考虑了还可以通过其他方式(例如隐式渲染方式)获得的动作。

参数

  • action_name - 要测试的动作的名称

# File actionpack/lib/abstract_controller/base.rb, line 184
def available_action?(action_name)
  _find_action_name(action_name)
end

controller_path()

委托给类的 ::controller_path

# File actionpack/lib/abstract_controller/base.rb, line 165
def controller_path
  self.class.controller_path
end

formats

返回控制器可以处理的格式。

# File actionpack/lib/abstract_controller/base.rb, line 46
attr_internal :formats

performed?()

测试是否设置了响应主体。用于确定是否需要在 AbstractController::Callbacks 中终止 process_action 回调。

# File actionpack/lib/abstract_controller/base.rb, line 191
def performed?
  response_body
end

process(action, *args)

调用操作,遍历整个 Action Dispatch 堆栈。

实际调用的方法由调用 method_for_action 确定。如果没有方法可以处理该操作,则会引发 AbstractController::ActionNotFound 错误。

返回

  • self

# File actionpack/lib/abstract_controller/base.rb, line 151
def process(action, *args)
  @_action_name = action.to_s

  unless action_name = _find_action_name(@_action_name)
    raise ActionNotFound.new("The action '#{action}' could not be found for #{self.class.name}", self, action)
  end

  @_response_body = nil

  process_action(action_name, *args)
end

response_body

返回控制器发送的 HTTP 响应的主体。

# File actionpack/lib/abstract_controller/base.rb, line 38
attr_internal :response_body