Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在脚本中插入Ruby环境[api_key]_Ruby On Rails_Ruby_Openweathermap - Fatal编程技术网

Ruby on rails 在脚本中插入Ruby环境[api_key]

Ruby on rails 在脚本中插入Ruby环境[api_key],ruby-on-rails,ruby,openweathermap,Ruby On Rails,Ruby,Openweathermap,在application.yml中,我有api_键:1234567890 如果我这样做(直接插入api键“appid”),它就会工作(小部件显示并填充): window.mywidgetpram?window.mywidgetpram:window.mywidgetpram=[];window.mywidgetpram.push({id:15,cityid:'2643743',appid:'1234567890'>,units:'metric',containerid:'openweather

在application.yml中,我有
api_键:1234567890

如果我这样做(直接插入api键“appid”),它就会工作(小部件显示并填充):


window.mywidgetpram?window.mywidgetpram:window.mywidgetpram=[];window.mywidgetpram.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);});
但如果我这样做(插入
),它不会显示(由于api键周围缺少引号,小部件不会显示):


window.mywidgetpram?window.mywidgetpram:window.mywidgetpram=[];push({id:15,cityid:'2643743',appid:,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);});
我遗漏了什么?

关于appid:“”?

我会做:

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


该文件的名称是什么,如何呈现?我打赌它的名称中没有
.erb
(home.html.erb;)定义“不起作用”。api密钥是否正确呈现在页面上?您可能会发现您忘记添加了一些内容。接下来的问题确实无关紧要/超出了范围。@SergioTulentsev这是否超出了范围:如何隐藏api密钥?@Johanfarch,这不是重点吗?你说它崩溃是因为缺少引号。另外,您的第一个示例中有
appid:'1234567890'
,而不是
appid:1234567890
,在您的建议中,它将实际代码作为字符串而不是api键插入。我的“第一个例子”来自yml文件,它有不同的语法-没有引号。我需要在引号中传递实际的api键,如我问题的第一个脚本所示。@JohanFaerch“只是作为字符串插入”——不,我不这么认为。一定是你在应用这个答案时做错了什么。对不起。误解了你的最后一部分。第一个脚本示例是正确的和有效的,但是直接在文件中输入了api密钥。在脚本中插入
“1234567890”
=
是ERB等价于不转义输出的代码。
<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>
<%== ENV['api_key'].to_json %>