在application.yml中我有api_key: 1234567890
如果我这样做(直接插入api键' appid'它)可以工作(小部件显示和填充):
<script>
window.myWidgetParam ? window.myWidgetParam : window.myWidgetParam = []; window.myWidgetParam.push({id: 15,cityid: '2643743',appid: '1234567890' %>,units: 'metric',containerid: 'openweathermap-widget-15', }); (function() {var script = document.createElement('script');script.async = true;script.charset = "utf-8";script.src = "//openweathermap.org/themes/openweathermap/assets/vendor/owm/js/weather-widget-generator.js";var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(script, s); })();
</script>
但是,如果我这样做(插入<%= ENV["api_key"] %>
)它没有(由于api键周围缺少引号,小部件无法显示):
<script>
window.myWidgetParam ? window.myWidgetParam : window.myWidgetParam = []; window.myWidgetParam.push({id: 15,cityid: '2643743',appid: <%= ENV["api_key"] %>,units: 'metric',containerid: 'openweathermap-widget-15', }); (function() {var script = document.createElement('script');script.async = true;script.charset = "utf-8";script.src = "//openweathermap.org/themes/openweathermap/assets/vendor/owm/js/weather-widget-generator.js";var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(script, s); })();
</script>
我错过了什么?
答案 0 :(得分:1)
我愿意:
<%== ENV['api_key'].to_json %>
答案 1 :(得分:0)
appid: '<%= ENV["api_key"] %>'
怎么样?