据说,ActionController::Base.helpers
充当访问 View 外部助手的代理。然而,那里定义的许多方法依赖于 Controller 变量,我无法成功调用:
ActionController::Base.helpers.image_path("my_image.png")
>> TypeError Exception: can't convert nil into String
在源代码中挖掘,我看到 compute_asset_host
方法正在尝试访问 config.asset_host
但 config
是 nil
.
如何从外部 View 成功调用 image_path
?
最佳答案
使用 view_context
访问 View 中可用的辅助方法。
您可以像这样从 Controller 调用image_path
。
view_context.image_path "my_image.png"
关于ruby-on-rails - AssetTagHelper::image_path 外部 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3804082/