【问题标题】:How can I access image_path route helper inside a Rails 3 model?如何在 Rails 3 模型中访问 image_path 路由助手?
【发布时间】:2012-10-27 00:44:09
【问题描述】:

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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2 asset-pipeline sprockets


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2011-04-07
      • 1970-01-01
      • 2016-02-14
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      • 2011-12-20
      • 1970-01-01
      相关资源
      最近更新 更多