【问题标题】:Gmaps is not defined in production on Heroku CedarHeroku Cedar 的生产环境中未定义 Gmaps
【发布时间】:2012-04-25 02:17:22
【问题描述】:

我是 RoR 的新手,所以有很多概念我还不太了解。

作为我在 Rails 上的第一个项目,我根据 Michael Hartl 的教程编写了一个网站。 我的网站是在 rails 3.1.0 中完成的,它在我的 ubuntu 站上的 dev 中运行良好。 在该站点中,我有 gmaps4rails,它以这种方式加载到 gemfile 中:

gem 'gmaps4rails', "~> 1.4.8" 

我还有另一个名为 timeglider 的控件,它是一个 javacript 控件,位于我的 /public/timeglider 文件夹中的公用文件夹中。

所有这些都在开发中工作,但是当我想在 heroku 上的生产中部署它时,我的 chrome 控制台出现问题:

http://geojiffy.herokuapp.com/:277Uncaught ReferenceError: Gmaps is not defined
http://geojiffy.herokuapp.com/:182Uncaught ReferenceError: Gmaps is not defined

我邀请你去看看加载页面的结果here。 gmaps 部分中没有加载任何内容。

问题是 Gmaps 在到达第 277 行时没有定义。

当我尝试修复它时,我在 stackoverflow 中阅读了许多其他关于 sprocket 及其编译 js 文件的方式的问题。

我将 application.js 从 *= require_tree . 更改为

//= require jquery-ui
//= require jquery
//= require jquery_ujs
//= require jiffies.js
//= require gmap3.min.js
//= require jquery-1.7.1.min.js
//= require jquery-ui-1.8.16.custom.min.js
//= require gmaps4rails/all_apis.js
//= require gmaps4rails/bing.js
//= require gmaps4rails/gmaps4rails.base.js.coffee
//= require gmaps4rails/gmaps4rails.bing.js.coffee
//= require gmaps4rails/gmaps4rails.googlemaps.js.coffee
//= require gmaps4rails/gmaps4rails.base.js
//= require gmaps4rails/googlemaps.js

脚本由 heroku 在创建 slug 时编译

您可以访问完整的 application.js here 您可以找到 Gmaps 对象在该脚本中的定义位置。

很明显,在 rails、gmaps4rails 和 heroku 中有很多我不理解的东西会导致部署问题。我不确定问题是否来自资产管道。

我在这个错误上花了很多晚上,但我无法理解调试它的清晰方法。你能给我一些提示来找到我的错误吗?

p.s : 我想给你发照片和链接,但我是新手,所以有限制

这是我认为的代码:

 <%= gmaps({ "map_options" => { "type" => "ROADMAP",  "zoom" => 10, "detect_location" => true,"center_on_user" => true,"auto_adjust" => false}, 
                "markers" => { "data" => @json } }) %> 
<div class='timeline-div' id='placement'></div>

$(function () { 
    var isGeoChanging = false;
    var lastGmapInfoWindowOpen = null; 
    var lastTgInfoWindowOpen = null;

    //*****************************************************
   // Code pour les évènements sur control du formulaire
   //*****************************************************
   $("#btn_change_geo").live('click', function() {
        switchGeoChanging();
    });


    switchGeoChanging = function()
    {
        if (isGeoChanging)
        { 
            $("#btn_change_geo").html("Change Jiffy's Geo");
            isGeoChanging = false;
            $("#div_change_geo_info").hide();
        }
        else
        {
            $("#btn_change_geo").html("Cancel");
            isGeoChanging = true;
            $("#div_change_geo_info").show();
        }

    }

    //************************************************
    // Code pour les évènements sur le gmap
    //************************************************
    Gmaps.map.callback = function() 
    { 
        for (var i=0; i<this.markers.length; i++)
        {                   
                var marker = Gmaps.map.markers[i].serviceObject;
                marker.set("id", Gmaps.map.markers[i].id);
                google.maps.event.addListener(marker, 'click', function()
                {   
                    //ici on cache les infowindow precedament ouvert pour garder la fenêtre à l'ordre
                    if (lastGmapInfoWindowOpen != null) { lastGmapInfoWindowOpen.infowindow.close();  }
                    lastGmapInfoWindowOpen = marker;
                    if (lastTgInfoWindowOpen != null) { lastTgInfoWindowOpen.hide();  }

                    //ici,si l'option est enclanché, on change la valeur du du geoId qui est associé au Jiffy
                    if (isGeoChanging)
                    {
                        switchGeoChanging();
                        $("#txt_geo_id").val(this.get("id"));
                        callAjaxGet("http://localhost:3000/jiffies/"+this.get('id')+"/getGeo");
                    }

                } );        
        }
        marker = null;
    };

    callAjaxGet = function(url)
    {
        $.ajax(url, {
                        type: 'GET',
                        data: {  },
                        success: function() { },
                        error: function() { alert("Impossible de charger le Jiffy."); }
                    }); 
    }


    //************************************************
    //  code pour les évènements sur le timeglider
    //************************************************
    $(".timeline-table").css({"display":"block"});


    var tg1 = $("#placement").timeline({
            "min_zoom":5, 
            "max_zoom":50, 
            "show_centerline":true,
            "data_source":"#jiffies_table",
            "show_footer":false,
            "display_zoom_level":true,
            "event_overflow":"scroll",
            "icon_folder":"http://localhost:3000/timeglider/js/timeglider/icons/"
    });





    $("#scrolldown").bind("click", function() {
        $(".timeglider-timeline-event").animate({top:"+=100"})
    });

    $("#scrollup").bind("click", function() {
        $(".timeglider-timeline-event").animate({top:"-=100"})
    });

    <%= @js_jiffies.html_safe%>



}); // end document-ready

【问题讨论】:

  • 你认为的代码是什么?
  • 澄清一下,这个 div
  • 我遇到了一个非常相似的问题。它开始发生在我开始弄乱 Heroku 上的资产管道和内存缓存的时候。将回滚到较早的提交,看看世界上正在发生什么。

标签: heroku production pipeline gmaps4rails assets


【解决方案1】:

查看网页http://geojiffy.herokuapp.com/,有许多javascript 文件的URL 路径类似于下面的代码。 http://localhost:3000 可以在你的开发机器上工作,但不能在 Heroku 上工作。

<script src="http://localhost:3000/timeglider/js/timeglider/TG_Date.js" type="text/javascript" charset="utf-8"></script> 
    <script src="http://localhost:3000/timeglider/js/timeglider/TG_Org.js" type="text/javascript" charset="utf-8"></script> 
    <script src="http://localhost:3000/timeglider/js/timeglider/TG_Timeline.js" type="text/javascript" charset="utf-8"></script> 
    <script src="http://localhost:3000/timeglider/js/timeglider/TG_TimelineView.js" type="text/javascript" charset="utf-8"></script> 
    <script src="http://localhost:3000/timeglider/js/timeglider/TG_Mediator.js" type="text/javascript" charset="utf-8"></script> 
    <script src="http://localhost:3000/timeglider/js/timeglider/timeglider.timeline.widget.js" type="text/javascript" charset="utf-8"></script> 

编辑。从 Chrome 的控制台

加载资源失败 http://localhost:3000/timeglider/css/aristo/jquery-ui-1.8.5.custom.cssFailed 加载资源 http://localhost:3000/timeglider/js/timeglider/Timeglider.cssFailed 到 加载资源 应用程序-a4d3485a82d7a76995c8b93a7477f17a.js:315Uncaught ReferenceError:未定义 jQuery http://localhost:3000/timeglider/js/jquery.jsFailed 加载资源 http://localhost:3000/timeglider/js/jquery-ui.jsFailed 加载 资源http://localhost:3000/timeglider/js/underscore-min.jsFailed 加载资源 http://localhost:3000/timeglider/js/backbone-min.jsFailed 加载 资源http://localhost:3000/timeglider/js/jquery.tmpl.jsFailed 到 加载资源 http://localhost:3000/timeglider/js/ba-debug.min.jsFailed 加载 资源http://localhost:3000/timeglider/js/ba-tinyPubSub.jsFailed 到 加载资源 http://localhost:3000/timeglider/js/jquery.mousewheel.min.jsFailed 到 加载资源 http://localhost:3000/timeglider/js/jquery.ui.ipad.jsFailed 加载 资源http://localhost:3000/timeglider/js/raphael-min.jsFailed 到 加载资源 http://localhost:3000/timeglider/js/jquery.global.jsFailed 加载 资源 http://localhost:3000/timeglider/js/timeglider/TG_Date.jsFailed 到 加载资源 http://localhost:3000/timeglider/js/timeglider/TG_Org.jsFailed 加载 资源 http://localhost:3000/timeglider/js/timeglider/TG_Timeline.jsFailed 到 加载资源 http://localhost:3000/timeglider/js/timeglider/TG_TimelineView.jsFailed 加载资源 http://localhost:3000/timeglider/js/timeglider/TG_Mediator.jsFailed 到 加载资源 http://localhost:3000/timeglider/js/timeglider/timeglider.timeline.widget.jsFailed 加载资源 http://geojiffy.herokuapp.com/assets/defaults.jsFailed 加载 资源:服务器响应状态为 404(未找到) http://geojiffy.herokuapp.com/:149Uncaught ReferenceError: $ 不是 定义http://geojiffy.herokuapp.com/:277UncaughtReferenceError: 未定义 Gmaps

【讨论】:

  • 你是正确的时间滑翔机部分谢谢你,但我仍然对 gmaps 错误有兴趣。
  • gmaps 未加载,因为 jQuery javascript 文件未加载。
  • 你说得对,更清楚一点,Gmaps 不需要 jQuery,但它需要加载文件。
【解决方案2】:

好的,这是一个初学者的问题,谢谢大家的提示。线索来自 jquery 参考错误。在资产管道中加载脚本时,您还必须注意加载顺序。您可以在那里看到类似的问题,但在 Asset Pipeline 中没有。
jquery is not define error 这就是我的应用程序中的 application.js 中的良好顺序:

//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require jquery-1.7.1.min.js 
//= require jquery-ui-1.8.16.custom.min.js
//= require jiffies.js
//= require gmaps4rails/all_apis.js
//= require gmaps4rails/bing.js
//= require gmaps4rails/gmaps4rails.base.js.coffee
//= require gmaps4rails/gmaps4rails.bing.js.coffee
//= require gmaps4rails/gmaps4rails.googlemaps.js.coffee
//= require gmaps4rails/gmaps4rails.base.js
//= require gmaps4rails/googlemaps.js
//= require gmap3.min.js//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require jquery-1.7.1.min.js 
//= require jquery-ui-1.8.16.custom.min.js
//= require jiffies.js
//= require gmaps4rails/all_apis.js
//= require gmaps4rails/bing.js
//= require gmaps4rails/gmaps4rails.base.js.coffee
//= require gmaps4rails/gmaps4rails.bing.js.coffee
//= require gmaps4rails/gmaps4rails.googlemaps.js.coffee
//= require gmaps4rails/gmaps4rails.base.js
//= require gmaps4rails/googlemaps.js
//= require gmap3.min.js

您可以看到第一个 jquery,然后是 jquery-ui,然后是 gmaps4rails/ 目录中的所有 api,最后是 gmaps.min.js

另外//= require_tree . 在生产中也不好。您必须写下所需的所有脚本路径://= 需要 PATH_TO_THE_SCRIPT。

谢谢

【讨论】:

    【解决方案3】:

    这完全符合逻辑:您的 application.js 文件无法加载,因为它们是一个错误“未捕获的 ReferenceError:jQuery 未定义”。

    这就是为什么Gmaps 之后没有定义的原因。修复你的 js 依赖项。

    【讨论】:

    • 知道是什么原因造成的吗?我遇到了类似的问题,在我的 application.js 中看起来一切应该没问题...
    • @tibbon:我猜你忘记了管道中的一些文件,或者你没有正确预编译它们
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签