Ruby on Rails 将google maps javascript标签添加到布局标题
示例
为了使GoogleMaps与Turbolinks正常工作,请将javascript标签直接添加到布局标题中,而不要在视图中包含它。
#app/views/layouts/my_layout.html.haml
!!!
%html{:lang => 'en'}
%head
- #...
= google_maps_api_script_tag将google_maps_api_script_tag在一个辅助的最好解释。
#app/helpers/google_maps_helper.rb
module GoogleMapsHelper
def google_maps_api_script_tag
javascript_include_tag google_maps_api_source
end
def google_maps_api_source
"https://maps.googleapis.com/maps/api/js?key=#{google_maps_api_key}"
end
def google_maps_api_key
Rails.application.secrets.google_maps_api_key
end
end您可以向Google注册您的应用程序,然后在GoogleAPI控制台中获取您的API密钥。Google提供了一个简短指南,介绍如何为GoogleMapsJavaScriptAPI请求api密钥。
api密钥存储在secrets.yml文件中:
#config/secrets.yml development: google_maps_api_key: '...' #... production: google_maps_api_key: '...' #...
不要忘记添加config/secrets.yml到.gitignore文件中,并确保没有将api密钥提交到存储库。
热门推荐
1 如何训练狗狗不叫
2 训练狗狗不叫
3 训练狗狗随行
4 怎么训练狗狗不叫
7 训练比熊狗狗教程
10 上海狗狗训练
11 怎么训练狗狗上厕所
12 训练狗狗趴下
13 狗狗服从训练
14 训练狗狗拒食
15 训练狗狗笑
16 训练狗狗大便
17 训练狗狗立
18 训练狗狗捡东西