Ruby on rails Rails ActionView::帮助程序未正确解析资源路径
我有一个类似于这样的小ruby模块/类:Ruby on rails Rails ActionView::帮助程序未正确解析资源路径,ruby-on-rails,ruby,ruby-on-rails-4,asset-pipeline,Ruby On Rails,Ruby,Ruby On Rails 4,Asset Pipeline,我有一个类似于这样的小ruby模块/类: module MyModule class MyClass include ActionView::Helpers::TagHelper include ActionView::Context include ActionView::Helpers::AssetUrlHelper def test puts "asset_url: #{asset_url('poster.jpg')}"
module MyModule
class MyClass
include ActionView::Helpers::TagHelper
include ActionView::Context
include ActionView::Helpers::AssetUrlHelper
def test
puts "asset_url: #{asset_url('poster.jpg')}"
puts "asset_path: #{asset_path('poster.jpg')}"
end
end
end
当我在控制器中创建该类的新实例,然后调用测试方法时,结果是:
asset_url: /poster.jpg
asset_path: /poster.jpg
asset_url: http://localhost:3000/assets/poster.jpg
asset_path: /assets/poster.jpg
这不是我想要/期望/希望的
但如果我这样做是为了:
= "asset_url: #{asset_url('poster.jpg')}"
= "asset_path: #{asset_path('poster.jpg')}"
结果是:
asset_url: /poster.jpg
asset_path: /poster.jpg
asset_url: http://localhost:3000/assets/poster.jpg
asset_path: /assets/poster.jpg
这正是我想要的!那么,为什么它在模块中给出不同的结果呢?如何在模块中获得正确的资产路径?谢谢 如果要使用控制器中视图和帮助程序中的方法,可以使用
view\u context
。
例如:
class MyController < ApplicationController
def index
logger.debug view_context.asset_url('poster.jpg')
logger.debug view_context.asset_path('poster.jpg')
end
end
或将方法委托给此对象(Ruby功能):
这可能只是因为模块的作用域中没有当前位置,没有根url。因此,当您调用该模块的方法时,它不知道要构建什么。在最坏的情况下,您可以硬编码路径,但我有兴趣看看其他人怎么说。