简体   繁体   English

Rails 3.2,jQuery 1.7.1到资产/图像的链接已损坏

[英]Rails 3.2, jQuery 1.7.1 link to assets/images broken

My setup includes ruby 1.9.3, Rails 3.2 and jQuery 1.7.1. 我的设置包括ruby 1.9.3,Rails 3.2和jQuery 1.7.1。 I found that jQuery is sending requests to /assets/images/image_file.png instead of just /assets/image_file.png which results into broken link as shown in the image below: 我发现jQuery将请求发送到/assets/images/image_file.png而不是/assets/image_file.png,这导致链接断开,如下图所示:

jQery链接到图像已损坏。资产中正确引用了其他CSS,JS和图像文件。

I know from Asset Organization that Rails look for appropriate folder in assets for js, css and image files. 我从资产组织知道,Rails在资产中为js,css和图像文件寻找适当的文件夹。 So I know that it should be looking in a subdirectory 'images' for the image referenced as /assets/images. 所以我知道它应该在子目录'images'中查找作为/ assets / images引用的图像。 I created an subdirectory images within in assets/images (app/assets/images/images) and copied the referenced image there and it worked. 我在资产/图像(应用程序/资产/图像/图像)中创建了一个子目录图像,然后将引用的图像复制到该目录中并且它可以正常工作。

But shouldn't jQuery reference the images correctly in assets directory. 但是jQuery不应正确引用资产目录中的图像。 Here is the jQuery code referenced by the error with line producing error highlighted: 这是错误所引用的jQuery代码,突出显示了产生行的错误:

jQuery代码导致链接断开,突出显示行产生错误。

Is there any configuration parameter that needs to be set in application.rb or environment files to set the asset path for jQuery? 是否需要在application.rb或环境文件中设置任何配置参数来设置jQuery的资产路径? Or any other way to fix this? 或其他解决方法?

在您的项目下的CSS周围搜索,并将images/ui-bg_glass_80_d7ebf9_1x400.pngui-bg_glass_80_d7ebf9_1x400.png ,希望它能正常工作。

This is happening because the jquery-ui-1.8.18.custom.css file expects its images to be in an images directory relative to CSS file itself. 发生这种情况是因为jquery-ui-1.8.18.custom.css文件期望其图像位于相对于CSS文件本身的images目录中。
However, the Rails 3 asset pipeline moves the CSS file to a different location so the image path no longer works. 但是,Rails 3资产管道将CSS文件移动到其他位置,因此图像路径不再起作用。

The standard layout if you download the jquery ui 'start' theme is: 如果下载jquery ui'start'主题,则标准布局为:

stylesheets/start/
stylesheets/start/jquery-ui-1.8.11.custom.css
stylesheets/start/images/

But the asset pipeline arranges it like this: 但是资产管道是这样安排的:

stylesheets/
stylesheets/jquery-ui-1.8.11.custom.css
stylesheets/start/images/

So the relative path of /images/ui-bg_glass_80_d7ebf9_1x400.png no longer works. 因此/images/ui-bg_glass_80_d7ebf9_1x400.png的相对路径不再起作用。

I have a workaround which is to add an Alias directive into my Apache config: 我有一个解决方法,即将Alias指令添加到我的Apache配置中:

Alias /assets/images "<path to site>/public/assets/start/images"

This assumes your assets are sym-linked into the public directory. 假设您的资产已符号链接到公共目录。
You could change that path to point directly to wherever your assets are served from. 您可以更改该路径以直接指向从何处提供资产。

This is a bit of a kludge as if I ever decide to change themes (highly unlikely) I'll need to update my Apache config. 好像有点犹豫,好像我曾经决定更改主题一样(极不可能),我需要更新我的Apache配置。
But for me it's better than rewriting the CSS file to change the paths as that becomes a pain when upgrading. 但是对我来说,比重写CSS文件来更改路径要好得多,因为升级时会很麻烦。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM