如何通过资产管道提供Javascript中子文件夹中的图像

How to serve images from subfolders in Javascript via Asset pipeline

本文关键字:文件夹 图像 Javascript 何通过 管道      更新时间:2023-09-26

所以我的assets/javascripts文件夹中有一个文件夹,它运行一个名为galleryview的图像幻灯片。在这个文件夹里是一个主题目录,里面有我的"下一个"answers"上一个"按钮的图像,因此。。。

assets/javascripts/galleryview/themes/

现在运行this的javascript,通过this链接到这些图像。。。。

//Determine path between current page and filmstrip images
//Scan script tags and look for path to GalleryView plugin
        $('script').each(function(i){
            var s = $(this);
            if(s.attr('src') && s.attr('src').match(/jquery'.galleryview/)){
                img_path = s.attr('src').split('jquery.galleryview')[0]+'themes/';
            }
        });

但是,我现在正在Rails应用程序中使用它,所以我需要将这条img_path行指向

资产/图像/

那么我的imag_path现在应该是什么样子呢?

img_path = ?

以下是jsfiddle上的整个javascript文件代码。。。。复制并传递到编辑器中我感兴趣的地区在389线附近。

http://jsfiddle.net/thefonso/Sqmxa/

不确定这段代码是否能正常工作,但我认为您可以使用image_path帮助程序来接近它。类似于:

img_path = <%= image_path 'images/'%>;

将其放入.js.erb文件中,上面显示的ERB应该被解析并输出字符串"/assets/images/",将img_path分配给值"/assets/themes/"

当然,你也可以做:

img_path = "/assets/images/";

但我想,使用helper的好处是,如果"assets"路径发生更改,则不需要更新硬编码字符串。