简体   繁体   English

如何查看环境变量?

[英]How do I view my environment variables?

I am trying to connect to my pusher server but am receiving the error: 我正在尝试连接到我的推送服务器但收到错误:

Missing client configuration: please check that key, secret and app_id are configured. 缺少客户端配置:请检查是否已配置密钥,密码和app_id。

I want to check my environmental variables, but cannot find any clear way to do this on Stack Overflow yet. 我想检查我的环境变量,但在Stack Overflow上找不到任何明确的方法。

Printing the Environment from the Shell 从命令行管理程序打印环境

As other answers have pointed out, one can use /usr/bin/env or /usr/bin/printenv from the command line to see what the environment is in the shell before starting Rails, or in a subshell after starting it. 正如其他答案所指出的,可以从命令行使用/usr/bin/env/usr/bin/printenv来查看在启动Rails之前shell中的环境,或者在启动它之后的子shell中。 For example: 例如:

  1. rails s RETURN rails s RETURN
  2. CTRL-Z CTRL-Z
  3. env RETURN env RETURN
  4. fg RETURN fg 返回

Displaying ENV from the View Layer 从视图层显示ENV

In Ruby, ENV is a "hash-like" accessor for environment variables; 在Ruby中, ENV是环境变量的“类哈希”访问器; it is not actually a Hash. 它实际上不是一个Hash。 You can introspect ENV from your Rails console easily enough simply by typing ENV or ENV['foo'] , but sometimes you may want to see what Rails thinks the environment is during rendering. 只需键入ENVENV['foo']就可以轻松地从Rails控制台中反省ENV,但有时您可能希望看到Rails在渲染过程中认为环境是什么。 In that case, you want the Rails debug helper . 在这种情况下,您需要Rails调试助手 For example: 例如:

# ERB
<%= debug ENV.to_h.to_yaml %>

# HAML
= debug ENV.to_h.to_yaml

Calling #to_yaml to serialize the ENV object will make the output easier to read, but requires you to convert ENV to a hash or array first. 调用#to_yaml来序列化ENV对象将使输出更容易读取,但需要首先将ENV转换为散列或数组。 You can also just invoke debug ENV without chaining; 你也可以不用链接调用debug ENV ; it's just harder on the eyes. 它只是在眼睛上更难。

或者在Ubuntu中使用O / S shell

printenv

Use command ENV in rails console. 在rails控制台中使用命令ENV That will return a hash of your environmental values you can access. 这将返回您可以访问的环境值的哈希值。 Alternatively, you can access your environmental variables from your apps root path using the same command and the variables will be returned formatted. 或者,您可以使用相同的命令从应用程序根路径访问环境变量,并且将返回格式化的变量。

这两个命令都将打印到stdout您的环境变量:

env

printenv

I have also used the following in the view layer: 我还在视图层中使用了以下内容:

<% request.env.each do |key, value| %>
  <strong><%= key %></strong> => <%= value %><br/>
<% end %>

I found it very helpful to debug issues caused by env variables set at the passenger/nginx level, that would not show up when I used rails console. 我发现调试由乘客/ nginx级别设置的env变量引起的问题非常有帮助,当我使用rails console时不会显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM