Ruby-on-rails 如何查看我的环境变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24685450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I view my environment variables?
提问by chopper draw lion4
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.
缺少客户端配置:请检查是否配置了 key、secret 和 app_id。
I want to check my environmental variables, but cannot find any clear way to do this on Stack Overflow yet.
我想检查我的环境变量,但在 Stack Overflow 上还找不到任何明确的方法来做到这一点。
回答by Todd A. Jacobs
Printing the Environment from the Shell
从 Shell 打印环境
As other answers have pointed out, one can use /usr/bin/envor /usr/bin/printenvfrom the command line to see what the environment is in the shell before starting Rails, or in a subshell after starting it. For example:
正如其他答案所指出的那样,可以使用/usr/bin/env或/usr/bin/printenv从命令行查看启动 Rails 之前 shell 中的环境,或者启动它之后的子 shell 中的环境。例如:
rails sRETURN- CTRL-Z
envRETURNfgRETURN
rails sRETURN- CTRL-Z
envRETURNfgRETURN
Displaying ENV from the View Layer
从视图层显示 ENV
In Ruby, ENVis a "hash-like" accessor for environment variables; it is notactually a Hash. You can introspect ENV from your Rails console easily enough simply by typing ENVor ENV['foo'], but sometimes you may want to see what Rails thinks the environment is during rendering. In that case, you want the Rails debug helper. For example:
在 Ruby 中,ENV是环境变量的“类哈希”访问器;它是不是真正的哈希值。您可以通过键入ENV或轻松地从 Rails 控制台内省 ENV ENV['foo'],但有时您可能想查看 Rails 在渲染过程中认为环境是什么。在这种情况下,您需要Rails 调试助手。例如:
# 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. You can also just invoke debug ENVwithout chaining; it's just harder on the eyes.
调用 #to_yaml 序列化 ENV 对象将使输出更易于阅读,但需要您先将 ENV 转换为散列或数组。你也可以只调用debug ENV而不链接;只是眼睛更难受。
回答by Michael Moulsdale
Or use the O/S shell, in Ubuntu use
或者使用O/S shell,在Ubuntu中使用
printenv
回答by chopper draw lion4
Use command ENVin rails console. 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.
ENV在 rails 控制台中使用命令。这将返回您可以访问的环境值的哈希值。或者,您可以使用相同的命令从您的应用程序根路径访问您的环境变量,并且这些变量将被格式化。
回答by jasonleonhard
Both of these commands will print to stdout your environment variables:
这两个命令都将打印到标准输出您的环境变量:
env
env
printenv
printenv
回答by crypticsymbols
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 级别设置的环境变量引起的问题非常有帮助,当我使用 rails 控制台时不会出现这些问题。

