Set a Rails environment variable locally (Mac OS X) - ruby-on-rails

I'm trying to set environment variables in Rails. I'm following these docs: http://railsapps.github.io/rails-environment-variables.html
One option it gives is to save environment variables in your ~/.bashrc, using this syntax:
export GMAIL_USERNAME="myname#gmail.com"
I tried adding exactly this to my ~/.bashrc. Then I stop my rails server, close my terminal, open my terminal, start rails server. The environment variable still doesn't seem to be available.
I've checked if it is available by doing rails console in my project root folder, and trying > ENV["GMAIL_USERNAME"] # => outputs nil
How can I set an environment variable locally (in development) so that my Rails project has access to it?

I dont know which shell you are using. In case of bash , you can write this in your ~/.bashrc file
export GMAIL_USERNAME=abc#bah.com
then do this in terminal
source ~/.bashrc
Now, check it console . I am sure it will be there .

Create a new file: config/initializers/settings.rb
GMAIL_USERNAME = case Rails.env
when 'development' then 'myname#gmail.com'
end
Restart your app and console.
You should be able to access it wherever you want:
> GMAIL_USERNAME
=> 'myname#gmail.com'

Related

how to add environment variable in linux for rails app?

i need to set an environment variable for the rails app to use
SECRET_KEY_BASE=9941144eb255ff0ffecasdlkjqweqwelkjasdlkjasd
the config settings for production is as shown below
# Do not keep production secrets in the repository,
# instead read values from the environment.
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
how can i set the environment variable using the linux command
export VARNAME="my value"
I tried to set the variable but looks like it needs to be for the right user. Sorry i am not an expert in linux.
I appreciate any help! Thanks!
export VARNAME="my value"
Well the above works for your current terminal session. After this command, all the subsequent commands can access this variable. Try running this:
echo $VARNAME
It will print the value my value in the console. If you want this behaviour to be persisted, you need to place the export command in your OS' config file (~/.bashrc in case of Ubuntu).
After editing this file, either restart your terminal, or run this:
source ~/.bashrc
This will reload the file in your current terminal session. Alternatively, you can try running your Rails server (or a rake command) as follows:
VARNAME="my value" rails s
For your local development I suggest you to use dotenv (https://github.com/bkeepers/dotenv) or figaro (https://github.com/laserlemon/figaro) and follow the README you find in the gem itself. This gives you much more flexibility than using directly environment variables because you set them only for this specific project and each project can have different of them.
You need to have either a .env file or a application.yml file where you will define your environment variables.
Remember to not commit or push this file to your repository because it contains sensible information!
When you will deploy to production you can use real environment variables or use admin panel control (on Heroku for example)

bashrc environment variables not working with Rails app

I have setup variables in my ~/.bashrc file that I would like to use with my Rails app. The problem is Rails will not recognize these variables.
bashrc:
export MYSQL_DB_USERNAME=admin
export MYSQL_DB_PASSWORD=testing123
Rails app - database.yml
username: <%= ENV["MYSQL_DB_USERNAME"] %>
password: <%= ENV["MYSQL_DB_PASSWORD"] %>
If I go into the rails console and type:
ENV["MYSQL_DB_USERNAME"]
I get back: <= nil
I've reloaded my bashrc file and restarted the terminal. Neither worked. Why won't Rails read these variables from the bashrc file?
(I am using RVM for ruby version management, in case that matters).
Thanks.
You can put your setting at the top of ~/.bashrc.
Because mina uses ssh in non-interactive mode.
In the first files of .bashrc, it has
#If not running interactively, don't do anything
[ -z "$PS1" ] && return
The settings below this line will be ignored.
It depends on how you are running the rails app.
For example if you did
export MYSQL_DB_USERNAME=admin
export MYSQL_DB_PASSWORD=testing123
rails console
> ENV["MYSQL_DB_USERNAME"]
Then most likely your code would work. If you are starting the railsapp as a different user, or as a daemon/service, then it won't share the same environment and the environment variables you created won't be set anymore.
In my opinion, the best way to set variables is to create a yaml config file. You can do /etc/myapp/config.yml and inside it set the following
MYSQL_DB_USERNAME: admin
MYSQL_DB_PASSWORD: testing123
This should be more cleaner than storing the user_name/password inside your .bashrc file since you can allow only the rails user to reading this through file permissions - you don't want to play with the file permissions of your .bashrc.

Ruby on Rails environment variable for development environment

Just a quick question to which I couldn't find an answer on stackoverflow.
If it is easy to have environment variable for staging and production (on heroku for example), how can I set environment variable for my localhost (development environment)? (I am on a mac)
As of today I hardcode my api credential for development environment and I don't feel comfortable with that.
Thanks !
Use dotenv is intended to be used in development:
Add your application configuration to your .env file in the root of your project.
S3_BUCKET=YOURS3BUCKET
SECRET_KEY=YOURSECRETKEYGOESHERE
You may also add export in front of each line so you can source the file in bash.
in .bashrc
export S3_BUCKET=YOURS3BUCKET
export SECRET_KEY=YOURSECRETKEYGOESHERE
Then access in rails app ENV['S3_BUCKET']
Environment variables are best placed in your .bash_profile file which lives in your home directory on the Mac: /Users/you/.bash_profile. Open that file and add something like this to the end of it:
export MY_ENV_VAR=my_env_value
or
export MY_ENV_VAR="a string with spaces in it"
export is a shell command that sets environment variables. Your .bash_profile is a bash script that runs every time you open a new shell session (open a terminal window) and therefore your export commands will run and set the env vars.
Then they will be available in the ENV constant when you're in Ruby.
Edit /Users/your_user_name/.bash_profile and add there:
export RAILS_ENV=development

env variables available in rails console but not in application

Never had such problem before with vps that I set up from zero, this one (Ubuntu 12.04, 64bit) was installed by some other developer.
The problem
in .bashrc file i have:
export FACEBOOK_ID=123456789
export FACEBOOK_SECRET=987654321
now in terminal if I type env I see these variables.
if I open rails console and type ENV["FACEBOOK_ID"] or ENV["FACEBOOK_SECRET"] I also can see the apropriate values.
The problem is that I have to use FACEBOOK_ID in the app in a view file and I do it with:
<%= ENV["FACEBOOK_ID"] %>
on local machine this returns the right value, in production on vps it returns nothing.
My idea is that the vps was not set up correctly, I couldnt find apache on it or ngnix, and the app is in var/www/apps/app_name/.
What could be wrong and how can I get this env variables in my template?
update
files available in root directory:
.bash_profile .bashrc .cshrc .zprofile .zshrc
Don't put it in local environment. What to do when you deploy the app? What to do when you want to develop another app on your local machine which use Facebook id as well?
Use Figaro gem. It's built for handling such case, env variables and private data. The env variables can be set in YAML file in app and won't be committed to repo. You won't regret.

Environment variable in Rails console and Pow

I can't access env variables in the Rails console, while in the application they work.
In .powenv I have export SENDGRID_PASSWORD="123"
In config/initializers/mail.rb there is:
ActionMailer::Base.smtp_settings = {
:password => ENV['SENDGRID_PASSWORD']
}
So in the console when I type UserMailer.welcome_mail.deliver there is an error 'ArgumentError: SMTP-AUTH requested but missing secret phrase'. However from the application it sends the mail successfully.
How can I make the env variables available in the console?
try
. .powenv
then
rails c
(dot is a command to run script on current environment)
Your Rails console isn't able to access the environment variable because Pow passes information from the .powenv or .powrc file into Rails ... Rails doesn't read those files on its own.
In other words, you're setting the ENV['SENDGRID_PASSWORD'] variable in the .powenv file, but that file is not being touched when you start the Rails console.
You'll need to set up a before_filter in your Application Controller that sets the ENV['SENDGRID_PASSWORD'] (or come up with another, similar, way of reading in the .powenv file from within that before_filter in your Rails app).
For posterity, you can add something like this to either your environment.rb, development.rb, or an initializer (config/initializers/pow.rb) depending on what load order you want:
# Load pow environment variables into development and test environments
if File.exist?(".powenv")
IO.foreach('.powenv') do |line|
next if !line.include?('export') || line.blank?
key, value = line.gsub('export','').split('=',2)
ENV[key.strip] = value.delete('"\'').strip
end
end

Resources