抽象控制器基类
AbstractController::Base
是一个低级 API。任何人都不应直接使用它,并且子类(如 ActionController::Base
)应提供自己的 render
方法,因为渲染根据上下文意味着不同的事物。
- A
- C
- F
- I
- M
- P
- R
- S
属性
[R] | abstract | |
[R] | abstract? |
类公共方法
abstract!() 链接
定义一个控制器为抽象。有关更多详细信息,请参阅 internal_methods
。
来源:显示 | 在 GitHub 上
# File actionpack/lib/abstract_controller/base.rb, line 57 def abstract! @abstract = true end
action_methods() 链接
应被视为操作的方法名称列表。这包括控制器上的所有公共实例方法,减去任何内部方法(请参阅 internal_methods
),添加回任何内部方法,但仍存在于类本身中。
返回
-
Set
- 应被视为操作的所有方法的集合。
来源:显示 | 在 GitHub 上
# 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
时,它们将被重新计算。
controller_path() 链接
返回完整的控制器名称,带下划线,不带结尾的 Controller。
class MyApp::MyPostsController < AbstractController::Base
end
MyApp::MyPostsController.controller_path # => "my_app/my_posts"
返回
-
字符串
internal_methods() 链接
控制器所有内部方法的列表。这将找到控制器的第一个抽象超类,并获取该抽象类上的所有公共实例方法的列表。控制器上的公共实例方法通常被视为动作方法,因此将删除在抽象类上声明的方法。(ActionController::Metal
和 ActionController::Base
被定义为抽象)
method_added(name) 链接
添加新的动作方法时,刷新缓存的 action_methods
。
supports_path?() 链接
如果给定的控制器能够呈现路径,则返回 true。AbstractController::Base
的子类可能返回 false。例如,电子邮件控制器不支持路径,只支持完整的 URL。
来源:显示 | 在 GitHub 上
# File actionpack/lib/abstract_controller/base.rb, line 199 def self.supports_path? true end
实例公共方法
action_methods() 链接
委托给类的 ::action_methods
。
来源:显示 | 在 GitHub 上
# File actionpack/lib/abstract_controller/base.rb, line 170 def action_methods self.class.action_methods end
action_name 链接
返回此控制器正在处理的动作的名称。
来源:显示 | 在 GitHub 上
# 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
- 要测试的动作的名称
来源:显示 | 在 GitHub 上
# File actionpack/lib/abstract_controller/base.rb, line 184 def available_action?(action_name) _find_action_name(action_name) end
controller_path() 链接
委托给类的 ::controller_path
。
来源:显示 | 在 GitHub 上
# File actionpack/lib/abstract_controller/base.rb, line 165 def controller_path self.class.controller_path end
formats 链接
返回控制器可以处理的格式。
来源:显示 | 在 GitHub 上
# File actionpack/lib/abstract_controller/base.rb, line 46 attr_internal :formats
performed?() 链接
测试是否设置了响应主体。用于确定是否需要在 AbstractController::Callbacks
中终止 process_action
回调。
来源:显示 | 在 GitHub 上
# 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
来源:显示 | 在 GitHub 上
# 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 响应的主体。
来源:显示 | 在 GitHub 上
# File actionpack/lib/abstract_controller/base.rb, line 38 attr_internal :response_body