【问题标题】:Use image_url or image_path in an observer class在观察者类中使用 image_url 或 image_path
【发布时间】:2011-12-18 09:24:13
【问题描述】:

我想从观察者类发布一些 Facebook 帖子,但是,我想从我的服务器中引用一张图片。过去我有一些非常老套的方法来做这件事,但现在有了新的资产,我虽然可以使用 image_path 或 image_url 方法。

但是,我似乎无法弄清楚如何调用这些。它们不是 ActionView::Helpers::AssetTagHelper 的类级别方法,这样做

ActionView::Helpers::AssetTagHelper.image_url

没用。

【问题讨论】:

    标签: ruby-on-rails observer-pattern


    【解决方案1】:

    首先,我认为image_url 不存在。我只看到image_pathin the API

    为了能够使用该方法,您必须包含 AssetTagHelper 模块

    include ActionView::Helpers::AssetTagHelper
    image_path('my_image.png')
    

    在 Rails 4 中包含 AssetUrlHelper:

    include ActionView::Helpers::AssetUrlHelper
    image_path('my_image.png')
    

    【讨论】:

    • @Matrix with rails 4 你应该可以这样做:ActionController::Base.helpers.image_path('my_image.png')
    • 其实image_url存在于Rails 4 API
    【解决方案2】:

    在 Rails 4 中,我们可以使用 image_urlhost 属性。

    正如上面提到的@piotrkaczmarek,我们需要包含AssetUrlHelper。

    include ActionView::Helpers::AssetUrlHelper
    image_url('image.jpg', host: 'http://example.com')
    

    【讨论】:

      猜你喜欢
      • 2019-06-19
      • 2021-09-25
      • 2013-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-23
      • 2012-04-20
      相关资源
      最近更新 更多