Gmaps在Heroku Cedar生产环境中未定义。

3

我对RoR还很陌生,所以有很多概念我还不是很理解。

作为我在Rails上的第一个项目,我按照Michael Hartl的教程编写了一个网站。我的网站使用的是Rails 3.1.0版本,在我的ubuntu电脑上开发时运行良好。

在我的网站中,我使用了gmaps4rails插件,通过gemfile引入:

gem 'gmaps4rails', "~> 1.4.8" 

我还有另一个叫做 timeglider控件,它是一个JavaScript控件,存放在我的公共文件夹中的 /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部分没有加载任何内容。
问题在于当它到达第277行时,Gmaps未被定义。
当我试图修复它时,我阅读了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这里,您可以在该脚本中找到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


你认为代码应该是什么样子的? - apneadiving
只是想澄清一下,当代码正常工作时,在我的页面中找到的这个<div id="map" class="gmaps4rails_map"...>是由视图或JavaScript中的gmaps4rails插件创建的? - Guillaume Lortie
我遇到了非常类似的问题。它开始发生在我开始在Heroku上搞资产管道和内存缓存的时候。我将回滚到早期的提交,看看到底发生了什么。 - tibbon
3个回答

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 无法加载资源 application-a4d3485a82d7a76995c8b93a7477f17a.js:315Uncaught 引用错误: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 引用错误:$未定义 http://geojiffy.herokuapp.com/:277Uncaught 引用错误:Gmaps未定义

你对Timeglider部分是正确的,谢谢,但我仍然卡在Gmaps错误上。 - Guillaume Lortie
gmaps无法加载是因为jQuery JavaScript文件未加载。 - Steve Wilhelm
你说得对,为了更清晰明了,Gmaps不需要jQuery,但它需要文件加载。 - apneadiving

1

好的,问题很简单,谢谢大家的提示。线索来自于jQuery引用错误。当您在资产管道中加载脚本时,还必须注意加载顺序。您可以在那里看到类似的问题,但不是在资产管道中。
jquery未定义错误 所以这是我的应用程序.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 .不太好用。您需要写下所有所需脚本路径://= require PATH_TO_THE_SCRIPT。

谢谢。


0

这是很合理的逻辑:你的application.js文件没有加载,因为有一个错误"Uncaught ReferenceError: jQuery is not defined"。

这就是为什么之后Gmaps未定义。修复你的js依赖关系。


有什么想法是什么原因导致的吗?我遇到了类似的问题,在我的application.js中,一切似乎都应该没问题... - tibbon
@tibbon:我猜你在管道中忘记了一些文件,或者没有正确地预编译它们。 - apneadiving

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接