使用 Rails 3.1 资产管道的 JavaScript 代码中的图像 URL? [英] URL of images in JavaScript code using Rails 3.1 asset pipeline?

查看:31
本文介绍了使用 Rails 3.1 资产管道的 JavaScript 代码中的图像 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 CSS 文件中,您可以使用以下方法获取图像资产的正确名称(带有指纹):

In CSS files, you can get the proper name of an image asset (with the fingerprint) by using:

background-image: url(image-url("rails.png"))

但是您如何从 JavaScript 文件中执行相同操作?

but how do you do the same from a JavaScript file?

推荐答案

我看到你正在使用 sass 辅助方法.

I see you are using the sass helper method.

在标准(非 Sass)CSS 中,您可以执行以下操作:

In standard (non Sass) CSS you do something like this:

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

CSS 文件需要将 erb 添加到扩展名中:

The CSS file will need to have erb added to the extensions:

file_name.css.erb

对于 javascript,同样的规则适用:

For javascript the same rules apply:

file_name.js.erb

并在文件中:

var image_path = '<%= asset_path 'image.png' %>'

Rails 资产管道指南是有关如何使用这些功能的绝佳信息来源.

The Rails asset pipeline guide is an excellent source of information about how to use these features.

这篇关于使用 Rails 3.1 资产管道的 JavaScript 代码中的图像 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆