我目前正在开发出现以下错误的 Rails 应用程序:
You have included the Google Maps API multiple times on this page. This may cause unexpected errors.
经过一些研究,我发现 Turbolinks 导致了这个问题。单击 link_to 时,Google map 创建的所有 DOM 元素都将保留在 DOM 中。呈现新页面时,会添加另一组 Google Map DOM 元素,从而导致重复和错误。
我可以通过简单地将 'data-no-turbolink' => true 添加到我的 link_to 来非常快速地解决这个问题,但这违背了使用 Turbolinks 的目的,因为它强制刷新。
我想知道是否有潜在的解决方法可以在不关闭 Turbolinks 的情况下停止这种复制?
map .js:
var initMap = function initMap() {
if (typeof mapLatLng != "undefined") {
// we can use this to set the map zoom
// in different places.
// use: window.setZoom = 12;
if (typeof setZoom ==! "undefined") {
var mapZoom = setZoom;
} else {
var mapZoom = 14;
}
var map = new google.maps.Map(document.getElementById('map'), {
zoom: mapZoom,
center: mapLatLng,
disableDefaultUI: true,
scrollwheel: false
});
var markerSVG = {
path: 'M1152 640q0-106-75-181t-181-75-181 75-75 181 75 181 181 75 181-75 75-181zm256 0q0 109-33 179l-364 774q-16 33-47.5 52t-67.5 19-67.5-19-46.5-52l-365-774q-33-70-33-179 0-212 150-362t362-150 362 150 150 362z',
fillColor: '#f32e74',
fillOpacity: 1,
strokeWeight: 0,
anchor: new google.maps.Point(870,1650),
scale: (0.02, 0.02)
};
var mapMarker = new google.maps.Marker({
position: map.getCenter(),
map: map,
icon: markerSVG,
});
}
}
查看:
<% if @job.address.latitude && @job.address.longitude %>
<%= javascript_tag do %>
window.mapLatLng = {lat: <%= @job.address.latitude %>, lng: <%= @job.address.longitude %>};
<% end %>
<% content_for :js do %>
<script async defer src="https://maps.googleapis.com/maps/api/js?signed_in=false&callback=initMap"></script>
<% end %>
<% end %>
最佳答案
而不是 <script async defer src="https://maps.googleapis.com/maps/api/js?signed_in=false&callback=initMap"></script>使其成为常规 <script></script>标记并在您的 initMap 函数下方添加以下内容:
if(window.google){
initMap();
} else{
$.ajax('https://maps.googleapis.com/maps/api/js?signed_in=false&callback=initMap', {
crossDomain: true,
dataType: 'script'
});
}
如果您不使用 jquery,则只需使用 XMLHttpRequest。
关于javascript - 防止由 Rails Turbolinks 导致的 Google maps JS 执行多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33115375/
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul
我在用Ruby执行简单任务时遇到了一件奇怪的事情。我只想用每个方法迭代字母表,但迭代在执行中先进行:alfawit=("a".."z")puts"That'sanalphabet:\n\n#{alfawit.each{|litera|putslitera}}"这段代码的结果是:(缩写)abc⋮xyzThat'sanalphabet:a..z知道为什么它会这样工作或者我做错了什么吗?提前致谢。 最佳答案 因为您的each调用被插入到在固定字符串之前执行的字符串文字中。此外,each返回一个Enumerable,实际上您甚至打印它。试试
如何检查Ruby文件是否是通过“require”或“load”导入的,而不是简单地从命令行执行的?例如:foo.rb的内容:puts"Hello"bar.rb的内容require'foo'输出:$./foo.rbHello$./bar.rbHello基本上,我想调用bar.rb以不执行puts调用。 最佳答案 将foo.rb改为:if__FILE__==$0puts"Hello"end检查__FILE__-当前ruby文件的名称-与$0-正在运行的脚本的名称。 关于ruby-检查是否
//1.验证返回状态码是否是200pm.test("Statuscodeis200",function(){pm.response.to.have.status(200);});//2.验证返回body内是否含有某个值pm.test("Bodymatchesstring",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});//3.验证某个返回值是否是100pm.test("Yourtestname",function(){varjsonData=pm.response.json
我从Ubuntu服务器上的RVM转移到rbenv。当我使用RVM时,使用bundle没有问题。转移到rbenv后,我在Jenkins的执行shell中收到“找不到命令”错误。我内爆并删除了RVM,并从~/.bashrc'中删除了所有与RVM相关的行。使用后我仍然收到此错误:rvmimploderm~/.rvm-rfrm~/.rvmrcgeminstallbundlerecho'exportPATH="$HOME/.rbenv/bin:$PATH"'>>~/.bashrcecho'eval"$(rbenvinit-)"'>>~/.bashrc.~/.bashrcrbenvversions
我有一个使用SeleniumWebdriver和Nokogiri的Ruby应用程序。我想选择一个类,然后对于那个类对应的每个div,我想根据div的内容执行一个Action。例如,我正在解析以下页面:https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=puppies这是一个搜索结果页面,我正在寻找描述中包含“Adoption”一词的第一个结果。因此机器人应该寻找带有className:"result"的div,对于每个检查它的.descriptiondiv是否包含单词“adoption
我写了一个非常简单的rake任务来尝试找到这个问题的根源。namespace:foodotaskbar::environmentdoputs'RUNNING'endend当在控制台中执行rakefoo:bar时,输出为:RUNNINGRUNNING当我执行任何rake任务时会发生这种情况。有没有人遇到过这样的事情?编辑上面的rake任务就是写在那个.rake文件中的所有内容。这是当前正在使用的Rakefile。requireFile.expand_path('../config/application',__FILE__)OurApp::Application.load_tasks这里
我目前正在用Ruby编写一个项目,它使用ActiveRecordgem进行数据库交互,我正在尝试使用ActiveRecord::Base.logger记录所有数据库事件具有以下代码的属性ActiveRecord::Base.logger=Logger.new(File.open('logs/database.log','a'))这适用于迁移等(出于某种原因似乎需要启用日志记录,因为它在禁用时会出现NilClass错误)但是当我尝试运行包含调用ActiveRecord对象的线程守护程序的项目时脚本失败并出现以下错误/System/Library/Frameworks/Ruby.frame