How do I see the ENV vars in a Rails app?

rails 5 environment variables
rails 6 environment variables
rails yml environment variables
check environment variables ruby
rails port environment variable
application.yml rails
rails env settings
dotenv

I am taking over an old Rails app. No one has touched it in a year. The last developer left in April of 2015 and I have no way to contact him. I do have ssh access to the server, and I have access to the Github repo.

I don't know any of the usernames/passwords.

If I ssh to the server and I cat the database.yml file, I see stuff like:

  staging:
        adapter: mysql2
        encoding: utf8
        pool: 5
        socket: /var/lib/mysql/mysql.sock
        database: o_wawa_stage
        username: wawa_stage
        password: <%= ENV['STAGE_DATABASE_PASSWORD'] %>
        host: access.dmedia.com

If I run the "printenv" command then I don't see any of these vars. I assume they are only loaded by the Rails environment.

I guess I can edit the templates to spit out the values with a bunch of "put" statements, but I'm thinking there must be a more obvious way to do this, other than printing the data where the public could see it?

If I try to run "rails console" I get:

  Rails Error: Unable to access log file. Please ensure that /var/www/haha/production/releases/20150118213616/log/development.log exists and is writable (ie, make it writable for user and group: chmod 0664 /var/www/haha/production/releases/20150118213616/log/development.log). The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.

I don't have sudo on this box, so I can not address the error.

Assuming the staging environment, as your example points to. You'll want to load the console by prepending the RAILS_ENV environment variable to the rails console command.

RAILS_ENV=staging rails console

That should get you in. Once you're in, you can just access the ENV variable directly.

2.2.2 (main):0 > ENV

And that will dump out the environment variables for you. Note, your prompt may look different. If you want to access a specific value, such as the database password, you can:

2.2.2 (main):0 > ENV['STAGE_DATABASE_PASSWORD']

The ENV object in Ruby - rubycademy, Setting your default editor; Telling Ruby where to find gems; Passing API keys into your application, without having to commit them to source control (git); Defining  Instead use the Ruby variable ENV ["GMAIL_USERNAME"] to obtain an environment variable. The variable can be used anywhere in a Rails application. Ruby will replace ENV ["GMAIL_USERNAME"] with an environment variable. Let’s consider how to set local environment variables.

Within your app directory, simply launch the Rails Console:

rails c

Then at the prompt:

2.3.0 :001 > ENV

This will list all loaded environmental variables for whichever environment you last exported.

Sorry, after posting this, I realized that the author had already tried to use rails console with errors...but I am fairly sure this should always work. You can't ask for printenv or env within the console, you must use all caps "ENV"

Using Ruby Environmental Variables, Rails Application Configuration File After setting env variables,we have to set local_env.yml file into config/application.rb file. RAILS_ENV is just an environmental variable which is set in the shell or the operating system itself (or when invoking the process). Environment variables are a set of dynamic named values that can affect the way running processes will behave on a computer. They are part of the environment in which a process runs.

yourapp/config/env.yml or application.yml etc...

Look for code that looks like

AWS_KEY_ID: blahblah23rkjewfojerflbah
AWS_SECRET_KEY_ID: blahblah2394082fkwejfoblah

Configuring Rails Applications, to hold configuration settings that should be made after all of the frameworks and plugins are loaded. If you use passenger_env_var command in your nginx config then environment variables will be setup by passenger, and thus accessible by your rails app. See my answer. – joshaidan Aug 10 '16 at 19:40

When you install the Figaro gem in your Rails app, any values that you enter into config/application.yml will be loaded into the ruby ENV hash on  Then create environment variables (in your .bashrc file for example) that store the sensivite data values. For example for your database: export MYAPP_DEV_DB_DATABASE=myapp_dev export MYAPP_DEV_DB_USER=username export MYAPP_DEV_DB_PW=secret Now, in your local box, you just refer to the environment variables whenever you need the sensitive data.

You can also set environment variables on an existing application by visiting the Environment  The env will only display a list of environment variables that have been exported and it will not show all bash variables. The set command allows you to change the values of shell options and set the positional parameters, or to display the names and values of shell variables. If no options or arguments are supplied, set displays the names and

Often when developing apps with Rails you need to set up environment variables to keep sensitive information secure in your code. This article  You can also edit config vars from your app’s Settings tab in the Heroku Dashboard: Using the Platform API. You can manage your app’s config vars programmatically with the Heroku Platform API using a simple HTTPS REST client and JSON data structures. You need a valid Heroku access token representing a user with proper permissions on the app. Accessing config var values from code

Comments
  • Is there a way I can tell "rails console" where to find the log? The log is at "/var/www/wawa/production/shared/log" but rails console is looking for it in "/var/www/wawa/production/log" and it throws an error because it can't find it.
  • Have a look inside of config/application.rb and config/enivornments/production.rb for any lines that look like config.logger. It's usually in one of those two files where logging configuration is overwritten. guides.rubyonrails.org/configuring.html might be helpful too.
  • That would only be true if we were using the Figaro gem, yes?
  • This returns nothing: find . -name application.yml
  • This returns nothing: find . -name env.yml
  • "find . -name *.yml" returns over a 100 files, but nothing with "application" or "env" in the name.
  • Yes figaro or envyous, although if the prev dev was using ENV variables they must be stored somewhere, unless the code was pushed to github in which you pulled from when that .yml file was inside of the .gitignore