抽象控制器 Base
AbstractController::Base
為低階的 API。不應該直接使用,而預期子類(如 ActionController::Base
)提供自己的 render
方法,因為 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 58 def abstract! @abstract = true end
action_methods() 連結
應該被視為動作的方法名稱清單。這包括控制器中所有的公共實例方法(減去所有內部方法,請參閱 internal_methods
),再將任何內部方法加回,但仍然存在於類別本身中。
傳回
-
Set
- 所有應該被視為動作的方法的集合。
來源: 顯示 | 在 GitHub 上
# File actionpack/lib/abstract_controller/base.rb, line 97 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
時,會重新計算這些方法。
來源: 顯示 | 在 GitHub 上
# File actionpack/lib/abstract_controller/base.rb, line 112 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"
傳回
-
字串
來源: 顯示 | 在 GitHub 上
# File actionpack/lib/abstract_controller/base.rb, line 127 def controller_path @controller_path ||= name.delete_suffix("Controller").underscore unless anonymous? end
internal_methods() 連結
列出控制器中所有內部方法,這將找到控制器的第一個抽象父類別,並從抽象類別中取得所有公用實例方法的清單。一般來說,控制器中的公用實例方法會被認為是動作方法,所以抽象類別中宣告的方法將被移除。(ActionController::Metal
和 ActionController::Base
已被定義為抽象)
原始碼: 顯示 | 在 GitHub 中
# 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_methods
。
原始碼: 顯示 | 在 GitHub 中
# File actionpack/lib/abstract_controller/base.rb, line 132 def method_added(name) super clear_action_methods! end
supports_path?() 連結
如果指定的控制器能夠呈現路徑,則傳回 true。AbstractController::Base
的子類別可能傳回 false。例如,電子郵件控制器不支援路徑,只支援完整的 URL。
原始碼: 顯示 | 在 GitHub 中
# File actionpack/lib/abstract_controller/base.rb, line 200 def self.supports_path? true end
實例公用方法
action_methods() 連結
委派給類別的 ::action_methods
。
原始碼: 顯示 | 在 GitHub 中
# File actionpack/lib/abstract_controller/base.rb, line 172 def action_methods self.class.action_methods end
action_name 連結
傳回控制器正在處理的動作名稱。
原始碼: 顯示 | 在 GitHub 中
# File actionpack/lib/abstract_controller/base.rb, line 44 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 187 def available_action?(action_name) _find_action_name(action_name) end
controller_path() 連結
委派給類別的 ::controller_path
。
原始碼: 顯示 | 在 GitHub 中
# File actionpack/lib/abstract_controller/base.rb, line 167 def controller_path self.class.controller_path end
formats 連結
傳回控制器可以處理的格式。
原始碼: 顯示 | 在 GitHub 中
# File actionpack/lib/abstract_controller/base.rb, line 48 attr_internal :formats
performed?() 連結
測試回應主體是否已設定。用於判斷是否需要在 AbstractController::Callbacks
中終止 process_action
回呼。
process(action, ...) 連結
呼叫執行整個 Action Dispatch 堆疊的動作。
實際呼叫的方法由 calling method_for_action 決定。如果沒有一個方法可以處理該動作,那麼會引發 AbstractController::ActionNotFound
錯誤。
傳回
-
本身
# File actionpack/lib/abstract_controller/base.rb, line 154 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