Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.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
Javascript 预编译application.js资产_Javascript_Jquery_Ruby On Rails_Ruby On Rails 3_Ruby On Rails 4 - Fatal编程技术网

Javascript 预编译application.js资产

Javascript 预编译application.js资产,javascript,jquery,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-4,Javascript,Jquery,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 4,如何让application.js中的代码正常工作,以便预编译此处使用的图像?我应该使用什么rails助手 $(document).ready(function(){ $(".imgthumb").click(function(){ $(".thumbnailcurrent").addClass("thumbnails"); $(".thumbnails").removeClass("thumbnailcurrent"); $(th

如何让application.js中的代码正常工作,以便预编译此处使用的图像?我应该使用什么rails助手

    $(document).ready(function(){
    $(".imgthumb").click(function(){
        $(".thumbnailcurrent").addClass("thumbnails");
        $(".thumbnails").removeClass("thumbnailcurrent");
        $(this).addClass("thumbnailcurrent");
        $(this).removeClass("thumbnails");

        var img=($(this).attr("data-id"));

        if (img==1) {

         $('.imgbig').html("<%= asset_path src: 'flappy1.jpg', height: '275', width: '391' %>");
        } else if (img==2) {

         $('.imgbig').html("<%= asset_path src: 'flappy2.jpg', height: '275', width: '391' %>");
        } else if (img==3) {

         $('.imgbig').html("<%= asset_path src: 'flappy3.jpg', height: '275', width: '391' %>");
        }


});

    });

位于公共/资产路径中的预编译映像?为什么不直接使用此路径?

您不能通过url访问您的资产:

'/assets/images/flappy1.png' 
因为在生产环境中,您会遇到以下情况:

“/assets/images/flappy1-b3bb250d5300736006c894.png”

它们之间的符合性表位于自动生成的清单文件中

然而,直接使用清单文件是一个坏主意,因为in包含许多无关数据

通常,您有两种选择:

使用Erb预处理器包装javascript,以访问

助手

或者更多的美容解决方案:gem'js_assets',解决您的问题。 gem安装后,您需要包含JavaScript帮助程序:

// app/assets/javascripts/application.js

//= require app_assets
符合性表将存储在window.project\u资产变量中

给定现有环境,asset_path javascript的helper方法接收资产的逻辑路径,并返回相对路径:

var path = asset_path('images/flappy1.jpg')
// the function will return for development:
// /images/flappy1.jpg
// and for production
// /assets/images/flappy1-b3bb250d5300736006c894.jpg

希望,这有帮助

当你说预编译图像时,你的意思是想从JS中预加载它们吗?我只希望上面的代码最终能正常工作。。它存储在applications.js.erb文件中。。你觉得合适吗?不确定我是否正确设置了资产路径和src:implementation。第二双眼睛会很棒,非常感谢;嘿,我想我理解你的问题。也许,这就是你需要的?是的,基本上图像是在开发中显示的,但不是在生产中。。那颗宝石会起作用吗?非常感谢。非常感谢Vitaly,这很公平,但是上面的代码看起来有用吗?我有一个图库,上面的代码中的图像仍然没有切换,我想确保它看起来是正确的。