跳到內文 跳到搜尋

抽象控制器 Base

AbstractController::Base 為低階的 API。不應該直接使用,而預期子類(如 ActionController::Base)提供自己的 render 方法,因為 render 視情況會有不同的意涵。

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

屬性

[R] abstract
[R] abstract?

類別公共方法

abstract!()

將控制器定義為抽象。詳情請參閱 internal_methods

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

action_methods()

應該被視為動作的方法名稱清單。這包括控制器中所有的公共實例方法(減去所有內部方法,請參閱 internal_methods),再將任何內部方法加回,但仍然存在於類別本身中。

傳回

  • Set - 所有應該被視為動作的方法的集合。

# 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 時,會重新計算這些方法。

# 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"

傳回

  • 字串

# 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::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_methods

# 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。

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

實例公用方法

action_methods()

委派給類別的 ::action_methods

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

action_name

傳回控制器正在處理的動作名稱。

# 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 - 要測試的動作名稱

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

controller_path()

委派給類別的 ::controller_path

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

formats

傳回控制器可以處理的格式。

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

performed?()

測試回應主體是否已設定。用於判斷是否需要在 AbstractController::Callbacks 中終止 process_action 回呼。

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

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

response_body

傳回控制器所發送 HTTP 回應的主體。

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