【问题标题】:How to get the assets root path?如何获取资产根路径?
【发布时间】:2014-09-07 10:24:24
【问题描述】:

我知道image_tag,它返回一个资源的html标签。如果没有给出绝对路径,则资源挂载在assets下。

这个资产的根路径如何?我想避免在我的应用程序中使用硬编码的File.join('assets','other_resource')

【问题讨论】:

  • 你试过asset_path()吗?

标签: ruby-on-rails ruby ruby-on-rails-4 asset-pipeline


【解决方案1】:

你在寻找什么,确切地说,还不清楚......

但是,假设您想要图像资产的相对路径,您可以使用image_path

image_path("other_resource.png") # => /assets/other_resource.png

如果你想要图片资源的绝对路径,可以使用image_url

image_url("other_resource.png") # => http://example.com/assets/other_resource.png

【讨论】:

    【解决方案2】:

    我想你可以这样做:

    File.join(Rails.root, 'app', 'assets','other_resource')
    

    但我通常更喜欢:

    asset_path
    

    您也可以在 CSS 和 JS 中使用 asset_path:即

    .class { background-image: url(<%= asset_path 'image.png' %>) }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-10
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 2021-04-24
      • 1970-01-01
      • 2013-11-29
      • 1970-01-01
      相关资源
      最近更新 更多