跳至内容 跳至搜索

Abstract Controller Base

AbstractController::Base 是一个底层 API。没有人应该直接使用它,并且子类(例如 ActionController::Base)应该提供自己的 render 方法,因为渲染的含义根据上下文而有所不同。

方法
A
C
F
I
M
P
R
S

Attributes

[R] abstract
[R] abstract?

类公共方法

abstract!()

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

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

action_methods()

一个 Set,包含应被视为操作的方法名称。这包括控制器上的所有公共实例方法,减去任何内部方法(请参阅 internal_methods),并重新添加在类本身上但仍存在的内部方法。

# File actionpack/lib/abstract_controller/base.rb, line 93
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.reject! { |m| m.start_with?("_") }
    methods.map!(&:name)
    methods.to_set
  end
end

clear_action_methods!()

action_methods 是缓存的,有时需要刷新它们。::clear_action_methods! 允许您这样做,以便下次运行时 action_methods 将重新计算。

# File actionpack/lib/abstract_controller/base.rb, line 109
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 121
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 77
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_method 时,刷新缓存的 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 194
def self.supports_path?
  true
end

实例公共方法

action_methods()

委托给类的 ::action_methods

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

action_name

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

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

available_action?(action_name)

如果存在可分派的操作方法,则返回 true,否则返回 false。

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

参数

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

controller_path()

委托给类的 ::controller_path

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

formats

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

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

performed?()

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

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

process(action, ...)

调用操作,经过整个 Action Dispatch 堆栈。

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

# File actionpack/lib/abstract_controller/base.rb, line 148
def process(action, ...)
  @_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, ...)
end

response_body

返回控制器发送的 HTTP 响应正文。

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