Rails 4 Javascript:图像路径出现问题 [英] Rails 4 Javascript: Having trouble with image paths

查看:148
本文介绍了Rails 4 Javascript:图像路径出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让背景图像每6秒更改一次,这总是404错误:

I'm trying to get the background image to change every 6 seconds and it's always a 404 error:

例如,以下内容出现在JS控制台中

For example, the below appears in the JS console

http://localhost:3000/app/assets/images/ggb_mist.png 404 (Not Found) 

为了简化这一点,我尝试了一个背景色来确保它正在工作。见下文:

To simplify this I just tried a background-color to make sure it was working. See below:

$(document).ready(function() {
  function testing() {
    $('.holder').css('background-color', 'red');
  };
});

上述方法有效。现在,当我尝试对图像(位于 app / assets / images / imagefilenamehere.png 中执行相同操作时,我总是收到404错误。我尝试过:

The above works. Now when I try to do the same with an image (which is located in app/assets/images/imagefilenamehere.png I always get a 404 error. I've tried:

filenamehere.png
../assets/images/filenamehere.png
../app/assets/images/filenamehere.png
../images/filenamehere.png

为什么赢了下面的工作是什么?

Why won't the below work?

$('.holder').css({'backgroundImage':'../assets/images/filenamehere.png'});

对此事项的任何意见表示赞赏。

Any input on the matter is appreciated.

推荐答案

这解决了我的问题:第2.3节。一个guide.rubyonrails.org/asset_pipeline.html

$('.holder').css("background-image","url(<%= asset_path('tree-rays.jpg') %>)");

请注意 tree-rays.jpg 位于app / assets / images(Rails 4 Applicat)离子)。

Note that tree-rays.jpg is in app/assets/images (Rails 4 Application).

这篇关于Rails 4 Javascript:图像路径出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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