【问题标题】:jquery-ujs has already been loaded railsjquery-ujs 已经加载 rails
【发布时间】:2014-01-12 00:44:40
【问题描述】:

所以我正在尝试将谷歌地图集成到我的 rails 应用程序中(请记住,这是我第一次这样做,而且我是 rails 初学者,我不太了解 Ruby 代码)。

地图代码

<!-- 
    You need to include this script on any page that has a Google Map.
    When using Google Maps on your own site you MUST signup for your own API key at:
        https://developers.google.com/maps/documentation/javascript/tutorial#api_key
    After your sign up replace the key in the URL below or paste in the new script tag that Google provides.
-->
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDRKcd8KX82m0q47qSGB9Ryh79jqzjOpAk&sensor=false"></script>

<script type="text/javascript">
    // When the window has finished loading create our google map below
    google.maps.event.addDomListener(window, 'load', init);

    function init() {
        // Basic options for a simple Google Map
        // For more options see: https://developers.google.com/maps/documentation/javascript/reference#MapOptions
        var mapOptions = {
            // How zoomed in you want the map to start at (always required)
            zoom: 12,

            // The latitude and longitude to center the map (always required)
            center: new google.maps.LatLng(45.495040, -73.614371), // Montreal

            // How you would like to style the map. 
            // This is where you would paste any style found on Snazzy Maps.
            styles: [{"stylers":[{"hue":"#16a085"},{"saturation":0}]},{"featureType":"road","elementType":"geometry","stylers":[{"lightness":80},{"visibility":"simplified"}]},{"featureType":"road","elementType":"labels","stylers":[{"visibility":"off"}]}]
        };
        // Get the HTML DOM element that will contain your map 
        // We are using a div with id="map" seen below in the <body>
        var mapElement = document.getElementById('map');

        // Create the Google Map using out element and options defined above
        var map = new google.maps.Map(mapElement, mapOptions);
    }
</script> </head> <body>
<!-- The element that will contain our Google Map. This is used in both the Javascript and CSS above. -->
<div class="innerborder">
          <div id="map"></div>        </div>

由于某种原因,每当我单击包含地图的页面时,它都会尝试加载 jquery-ujs 两次。这是我的 application.js 包含树

//= 需要 jquery //= 需要 jquery_ujs //= 需要 turbolinks //=require_tree .

奇怪,不断收到这个: 未捕获的错误:jquery-ujs 已经加载!

对我有什么想法吗?

谢谢!

【问题讨论】:

  • require_tree . 正在加载目录树中的所有文件。
  • 嗨,删除 require_tree 。还没有解决这个问题。我仍然收到未捕获的错误:jquery-ujs 已经加载! jquery.js?body=1:517 未捕获的错误:jquery-ujs 已经加载! jquery.js?body=1:517 未捕获的错误:jquery-ujs 已经加载! jquery.js?body=1:517 不过谢谢!
  • 您是否也在使用包含脚本的 gem?
  • 我不知道。有没有办法找出来?这是我的个人宝石列表 # 个人宝石 gem 'activerecord-tableless' gem 'compass-rails', '~> 2.0.alpha.0' gem 'figaro' gem 'gibbon' gem 'google_drive' gem 'simple_form' gem 'better_errors' gem 'quiet_assets' gem "font-awesome-rails" 再次感谢!
  • @HarrisRobin U 发现问题??我也一样

标签: jquery ruby-on-rails google-maps


【解决方案1】:

我认为它来自 turbolinks。尝试禁用它们。我和他们有很多问题,最后把他们从一个项目中删除了。您可以使用此 gem 来解决问题,但如果您仍想尝试,请先禁用它们: https://github.com/kossnocorp/jquery.turbolinks

【讨论】:

  • 我认为与 Backbone、Ember Angular 等类似...我不认为 turbolinks 应该默认包含在 rails 中。感谢您的帮助,这对我有用:)
【解决方案2】:

我也遇到了这个问题,看起来@Cremz 对 turbolinks 是正确的。我找到了问题here 的解决方案,但简而言之,您很可能会将涡轮链接加载到正文中,如果是这样,请将它们移动到头部标签中。

【讨论】:

  • 这是我的确切问题。谢谢!
【解决方案3】:

我不想碰任何 gem 或配置,因为其他一切都工作正常,所以我写了一些 js 来重新加载页面,如果在 window.onpopstate 被触发时 turbolink 对象 restoreIdentifier 被复制

$(document).on('ready',function(){
	var turbi=window.history.state.turbolinks.restorationIdentifier;
	window.onpopstate= function(){
		if(window.history.state.turbolinks.restorationIdentifier==turbi){
			window.location.reload();
		}
	}
    
    });

【讨论】:

    猜你喜欢
    • 2014-01-08
    • 1970-01-01
    • 2017-05-10
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2018-02-09
    相关资源
    最近更新 更多