ruby-on-rails - AssetTagHelper::image_path 外部 View

标签 ruby-on-rails helper

据说,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_hostconfignil .

如何从外部 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/

相关文章:

ruby-on-rails - Acts_as_taggable_on 索引太长

ruby-on-rails - file_field 浏览按钮自定义

ruby-on-rails - Rails 和 Postgres : Finding items that are a "good deal"

ruby-on-rails - 通过子域共享一个Cookie

javascript - 在 Angular 应用程序外部分配范围对象

ruby-on-rails - 如何在 Ruby 中使用 RbVmomi 调整 VM 磁盘大小

macos - STPrivilegedTask 要求为每个任务输入密码

codeigniter - Codeigniter 中的辅助类 - 它们真的很糟糕吗?为什么?

laravel - 我正在使用 Laravel 5.2,是否有一些最佳建议将仅在 1 个 Controller 中使用的辅助函数放在哪里,

java - 尝试/捕获异常未按计划工作