ENV variables not reflecting in Rails code.
When you are developing in Rails 4, you might have seen that some ENV variables despite being set, will not be available in the program for e.g. rails console or rails server.
For example, you might have some code like,
This is because Rails 4 comes with Spring, which caches the code in memory and whenever you open a rails console or start the rails server, it will fork a process from the master process.
If you hadn't set the environment variables before the Spring server started running, then the master process will not have the environment variables available in its memory. And the forked child processes will never be able to see them.
To fix this, stop the Spring server using the command `./bin/spring stop`. This will stop the server and the environment variables will be available thereafter.
Note: If you setting some environment variables by typing `export GMAIL_USER_NAME=mygmail_username@gmail.com`, then it will only be available for that particular terminal (tab). Other terminal tabs/windows cannot see them. You should probably use ~/.bashrc or ~/.bash_profile based on your OS and Terminal, to avoid setting up environment variables every time.
For example, you might have some code like,
ActionMailer::Base.smtp_settings = {
....
user_name: ENV['GMAIL_USER_NAME']
password: ENV['GMAIL_PASSWORD']
....
}
but would have forgot to set the GMAIL_USER_NAME or GMAIL_PASSWORD environment variables. Now you would have tried exiting the rails console and setting the environment. But it still will not be available in the rails console.This is because Rails 4 comes with Spring, which caches the code in memory and whenever you open a rails console or start the rails server, it will fork a process from the master process.
If you hadn't set the environment variables before the Spring server started running, then the master process will not have the environment variables available in its memory. And the forked child processes will never be able to see them.
To fix this, stop the Spring server using the command `./bin/spring stop`. This will stop the server and the environment variables will be available thereafter.
Note: If you setting some environment variables by typing `export GMAIL_USER_NAME=mygmail_username@gmail.com`, then it will only be available for that particular terminal (tab). Other terminal tabs/windows cannot see them. You should probably use ~/.bashrc or ~/.bash_profile based on your OS and Terminal, to avoid setting up environment variables every time.
