AssetTagHelper :: image_path外部视图 [英] AssetTagHelper::image_path outside views
问题描述
据推测,ActionController::Base.helpers
的作用类似于访问外部视图中的帮助器的代理.但是,在那里定义的许多方法都依赖于控制器变量,而我无法成功调用:
Supposedly, ActionController::Base.helpers
acts like a proxy for accessing helpers outside views. However many of the methods defined there rely on controller variables and I'm unable to succesfully call:
ActionController::Base.helpers.image_path("my_image.png")
>> TypeError Exception: can't convert nil into String
从源头开始挖掘,我看到compute_asset_host
方法正在尝试访问config.asset_host
,但是config
是nil
.
Digging at the source I see compute_asset_host
method is trying to access config.asset_host
but config
is nil
.
如何从外部视图成功调用image_path
?
How can I successfully call image_path
from outside views?
推荐答案
使用view_context
访问视图中可用的那些辅助方法.
Use view_context
to access those helper methods that are available in the view.
您可以像这样从控制器调用image_path
.
You can call image_path
like this from the controller.
view_context.image_path "my_image.png"
这篇关于AssetTagHelper :: image_path外部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!