Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何获取资产根路径?_Ruby On Rails_Ruby_Ruby On Rails 4_Asset Pipeline - Fatal编程技术网

Ruby on rails 如何获取资产根路径?

Ruby on rails 如何获取资产根路径?,ruby-on-rails,ruby,ruby-on-rails-4,asset-pipeline,Ruby On Rails,Ruby,Ruby On Rails 4,Asset Pipeline,我知道,它返回一个资源的html标记。如果未给出绝对路径,则资源将装载在资产下 如何创建此资源根路径?我希望避免在我的应用程序中使用硬编码的文件。join('assets','other_resource')。我想您可以: File.join(Rails.root, 'app', 'assets','other_resource') 但我通常更喜欢: asset_path 您也可以从CSS和JS中使用:即 .class { background-image: url(<%= asset

我知道,它返回一个资源的html标记。如果未给出绝对路径,则资源将装载在
资产下


如何创建此资源根路径?我希望避免在我的应用程序中使用硬编码的
文件。join('assets','other_resource')

我想您可以:

File.join(Rails.root, 'app', 'assets','other_resource')
但我通常更喜欢:

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

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

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

但是,假设需要图像资源的相对路径,可以使用:

如果需要图像资源的绝对路径,可以使用:


您是否尝试了
资产路径()
image_path("other_resource.png") # => /assets/other_resource.png
image_url("other_resource.png") # => http://example.com/assets/other_resource.png