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密钥提交到存储库。
热门推荐
6 破五的祝福语简短
10 送偶像生日祝福语简短
11 中秋祝福语简短带署名
12 朋友出国旅游祝福语简短
13 中考祝福语给同学简短
14 牛年公司新年祝福语简短
15 对情侣朋友祝福语简短
16 晚来的生日祝福语简短
17 考进高中祝福语简短
18 部门过年祝福语简短独特