17

我正在尝试在 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")
4

1 回答 1

37

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

ActionController::Base.helpers.asset_path("image.png")

这似乎比创建一个新的 ActionController::Base 实例并请求视图上下文更可取,因为它更明确。

于 2013-03-29T01:52:10.563 回答