我有我的set_pagination_header
上應用控制器,我需要一些其他的控制器稱之爲:Rails:如何從控制器的before_action/after_action方法INLINE中調用ApplicationController方法WITH ARGS?
# syntax error in line below
after_action :set_pagination_header, :my_argument_object, only: [:index]
def index
@my_argument_object = Resource.page(1)
render json: @my_argument_object
end
我可以直接發送原始@my_argument_object
,所以我不想做的事:
def set_pagination_header(name, options = {})
scope = instance_variable_get("@#{name}")
# some_logic_here
end
1)什麼是正確的語法(需要一個內聯的)?
2)爲什麼不是set_pagination_header(object, options = {})
是什麼定義?
編輯:我發現了一些here像
after_filter only: [:index] { set_pagination_header(:germs) }
它的老語法,我們得到了after_action現在。儘管如此,我相信有一個內聯方式來做到這一點,不是嗎?
# I tried and that didn't work
after_action :set_pagination_header(:my_argument_object), only: [:index]
after_action { :set_pagination_header(:my_argument_object) } only: [:index]
after_action { :set_pagination_header, :my_argument_object } only: [:index]
,仍然沒有想出是否有可能發送哈希作爲!參數,而不僅僅是字符串名稱,它會讓我選擇使用該字符串的正確引用 –