Ruby在脚本中插入ENV [api_key]

时间:2018-04-25 10:15:05

标签: ruby-on-rails ruby openweathermap

在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>

我错过了什么?

2 个答案:

答案 0 :(得分:1)

我愿意:

<%== ENV['api_key'].to_json %>

答案 1 :(得分:0)

appid: '<%= ENV["api_key"] %>'怎么样?