方法
- #
- C
- H
- I
- M
屬性
[W] | _helpers |
執行個體公用方法
all_helpers_from_path(路徑) 連結
傳回給定路徑中幫手名稱的清單。
ActionController::Base.all_helpers_from_path 'app/helpers'
# => ["application", "chart", "rubygems"]
來源: on GitHub
# File actionpack/lib/abstract_controller/helpers.rb, line 93
_helpers_for_modification() 連結
clear_helpers() 連結
清除此類別中的所有現有幫手,只留下與此類別名稱相同的幫手。
# File actionpack/lib/abstract_controller/helpers.rb, line 209 def clear_helpers inherited_helper_methods = _helper_methods self._helpers = Module.new self._helper_methods = Array.new inherited_helper_methods.each { |meth| helper_method meth } default_helper_module! unless anonymous? end
helper(*args, &區塊) 連結
將給定的模組包含在範本類別中。
模組可以用不同的方式指定。下列所有呼叫皆包含 FooHelper
# Module, recommended.
helper FooHelper
# String/symbol without the "helper" suffix, camel or snake case.
helper "Foo"
helper :Foo
helper "foo"
helper :foo
最後兩者假設 "foo".camelize
傳回 “Foo”。
若傳入字串或符號,方法會使用 String#constantize
找出實際的模組物件。因此,如果模組尚未載入,它必須是自動可載入的,這通常是情況。
支援命名空間。下列呼叫包含 Foo::BarHelper
# Module, recommended.
helper Foo::BarHelper
# String/symbol without the "helper" suffix, camel or snake case.
helper "Foo::Bar"
helper :"Foo::Bar"
helper "foo/bar"
helper :"foo/bar"
最後兩者假設 "foo/bar".camelize
傳回 “Foo::Bar”。
方法也接受區塊。如果存在,會在控制器幫手模組的內容中評估區塊。這個簡單的呼叫會讓 wadus
方法在包覆控制器範本時可用
helper do
def wadus
"wadus"
end
end
此外,以上所有樣式都可以混合在一起
helper FooHelper, "woo", "bar/baz" do
def wadus
"wadus"
end
end
helper_method(*方法) 連結
宣告控制器方法為幫手。例如,下列會讓視圖可用 current_user
和 logged_in?
控制器方法
class ApplicationController < ActionController::Base
helper_method :current_user, :logged_in?
private
def current_user
@current_user ||= User.find_by(id: session[:user])
end
def logged_in?
current_user != nil
end
end
在視圖中
<% if logged_in? -%>Welcome, <%= current_user.name %><% end -%>
參數
-
方法[, 方法]
- 要在視圖中提供的控制器上的方法的名稱。
# File actionpack/lib/abstract_controller/helpers.rb, line 128 def helper_method(*methods) methods.flatten! self._helper_methods += methods location = caller_locations(1, 1).first file, line = location.path, location.lineno methods.each do |method| # def current_user(...) # controller.send(:'current_user', ...) # end _helpers_for_modification.class_eval <<~ruby_eval.lines.map(&:strip).join(";"), file, line def #{method}(...) controller.send(:'#{method}', ...) end ruby_eval end end
inherited(類別) 連結
一個類別被繼承時,將其輔助模組包裝在一個新的模組中。這確保了父類別的模組可以獨立於子類別而改變。
資料來源: 顯示 | 在 GitHub 上
# File actionpack/lib/abstract_controller/helpers.rb, line 68 def inherited(klass) # Inherited from parent by default klass._helpers = nil klass.class_eval { default_helper_module! } unless klass.anonymous? super end
modules_for_helpers(modules_or_helper_prefixes) 連結
傳入一個陣列的值,例如被 helper
接受的,這個方法會傳回一個陣列,其中包含對應的模組,順序與傳入值相同。
ActionController::Base.modules_for_helpers(["application", "chart", "rubygems"])
# => [ApplicationHelper, ChartHelper, RubygemsHelper]
資料來源: 在 GitHub 上
# File actionpack/lib/abstract_controller/helpers.rb, line 81