简体   繁体   中英

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.

I want to check my environmental variables, but cannot find any clear way to do this on Stack Overflow yet.

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. For example:

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

Displaying ENV from the View Layer

In Ruby, ENV is a "hash-like" accessor for environment variables; it is not actually a 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. In that case, you want the Rails debug helper . 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. You can also just invoke debug ENV without chaining; it's just harder on the eyes.

或者在Ubuntu中使用O / S shell

printenv

Use command ENV in 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.

这两个命令都将打印到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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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