如何在Rails 3模型中访问image_path路由助手?

SFn*_*Fnx 15 ruby-on-rails ruby-on-rails-3 sprockets asset-pipeline ruby-on-rails-3.2

我正在尝试从Rails 3模型中使用Sprockets的image_path方法.我已经尝试ActionView::Helpers::AssetTagHelper在我的模型中包含并调用image_path,但这不起作用(undefined local variable or method 'config').

有任何想法吗?

更新 - 这对我有用:

ActionController::Base.new.view_context.asset_path("image.png")
Run Code Online (Sandbox Code Playgroud)

Ala*_*n H 34

我认为这是加载资产助手的标准方法:

ActionController::Base.helpers.asset_path("image.png")
Run Code Online (Sandbox Code Playgroud)

这似乎比创建ActionController :: Base的新实例更好,并且要求视图上下文更明确.