AssetTagHelper :: image_path外部视图 [英] AssetTagHelper::image_path outside views

查看:73
本文介绍了AssetTagHelper :: image_path外部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据推测,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,但是confignil.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆