I know that I can set my ENV variables in bash via
export admin_password = \"secret\"
But is there a way to do it in my rails source code s
[Update]
While the solution under "old answer" will work for general problems, this section is to answer your specific question after clarification from your comment.
You should be able to set environment variables exactly like you specify in your question. As an example, I have a Heroku app that uses HTTP basic authentication.
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :authenticate
def authenticate
authenticate_or_request_with_http_basic do |username, password|
username == ENV['HTTP_USER'] && password == ENV['HTTP_PASS']
end
end
end
# config/initializers/dev_environment.rb
unless Rails.env.production?
ENV['HTTP_USER'] = 'testuser'
ENV['HTTP_PASS'] = 'testpass'
end
So in your case you would use
unless Rails.env.production?
ENV['admin_password'] = "secret"
end
Don't forget to restart the server so the configuration is reloaded!
[Old Answer]
For app-wide configuration, you might consider a solution like the following:
Create a file config/application.yml
with a hash of options you want to be able to access:
admin_password: something_secret
allow_registration: true
facebook:
app_id: application_id_here
app_secret: application_secret_here
api_key: api_key_here
Now, create the file config/initializers/app_config.rb
and include the following:
require 'yaml'
yaml_data = YAML::load(ERB.new(IO.read(File.join(Rails.root, 'config', 'application.yml'))).result)
APP_CONFIG = HashWithIndifferentAccess.new(yaml_data)
Now, anywhere in your application, you can access APP_CONFIG[:admin_password]
, along with all your other data. (Note that since the initializer includes ERB.new
, your YAML file can contain ERB markup.)