I'm trying to set a rails environment variable in a separate yml file. and it looks like this:
test:
sap_url: "http://example.com/"
development:
show_evp: 'true'
show_social_media: "true"
production:
show_evp: 'true'
show_social_media: "true"
staging:
show_evp: 'true'
show_social_media: "true"
And in my rails admin I do the following
config.model Settings do
weight 1
list do
field :id
field :show_hot_jobs
if ENV['SHOW_EVP'] == 'true'
field :show_evp
end
field :show_students
field :show_testimonials
field :show_on_boarding
end
But the variable is always false, anyone have an idea of where I need to set these rails env variables or what is wrong with mine.
Kr,
Vincent
Use Figaro gem to handle ENV variables and private data. Simple and easy. You won't regret.
Thanks for your help. I have created a .yml file in the config folder added all the settings that I need for dev / staging / prod.
Next in the lib folder created a module and did the following:
def self.config
OpenStruct.new \
:url => yaml["url"]
end
and now I can use it via "ModuleName".config.url
Kr,
Vince
Related
In my project there is a YAML file for NewRelic newrelic.yml that has a line with Ruby code for setting app_name. The existing config is working, and I need to edit the file, but when I try to do a yarn commit it fail with the following error, that to me looks like a Prettier error.
I have tried to add #prettier-ignore before the line, but it still causes the commit to fail.
node_modules/prettier/index.js:13588
throw error;
^
SyntaxError: Nested mappings are not allowed in compact mappings (18:13)
17 | # Relic. For more details, see https://docs.newrelic.com/docs/apm/new-relic-apm/maintenance/renaming-applications
> 18 | app_name: <%= ENV["SERVER_ENV"] == 'staging' ? 'MyApp (Staging)' : 'MyApp' %>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
My code editor is VSCode incase that is relevant.
So I'd like to either figure out why the # prettier-ignore is not working as the prettier docs indicate it should. Or figure out how to get things formatted to pass, which is the preferred option obviously.
Seems like Prettier doesn't handle ERB in YAML files.
I would add all common configurations into one block and would then re-use that block with YAML aliases.
common: &default_settings
license_key: 1234
log_lever: info
# ...
production:
<<: *default_settings
app_name: MyApp
staging:
<<: *default_settings
app_name: MyApp (Staging)
I want to set SECRET_KEY_BASE which is used in secrets.yml:
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
I have tried to add code as follow in .profile:
export SECRET_KEY_BASE=cfbc3b45d65db30b853cdc0557e0be85609cf75974ebb706f46a00abe09eee9454b3d311e48ee4157e1e5d5e3de5b8d2a329dff13871837cbaeae6af2bc2e42f
it works well, but this is still not that better, I know that dotenv can add this in a .env file in root path of app, so I add
gem 'dotenv-rails'
gem 'dotenv-deployment'
then I add code as follow into .env.production in root path of rails app:
SECRET_KEY_BASE=cfbc3b45d65db30b853cdc0557e0be85609cf75974ebb706f46a00abe09eee9454b3d311e48ee4157e1e5d5e3de5b8d2a329dff13871837cbaeae6af2bc2e42f
But why this doesn't work?
In Rails 4.1, config/secrets.yml is the new default location for secret_key_base of your application. It can, however, be used also for storing other secret variables, making it a good place for environment-specific tokens, API keys etc.
Fill the file with the secrets you want to store, for example:
development:
secret_key_base: your_development_secret
api_key: some_key
production:
secret_key_base: your_production_secret
twitter_consumer_key: production_twitter_key
twitter_consumer_secret: production_twitter_secret
twitter_oauth_token: production_oauth_token
twitter_oauth_token_secret: production_oauth_secret
In your code, you can access these secrets with Rails.application.secrets:
Twitter.configure do |config|
config.consumer_key = Rails.application.secrets.twitter_consumer_key
config.consumer_secret = Rails.application.secrets.twitter_consumer_secret
config.oauth_token = Rails.application.secrets.twitter_oauth_token
config.oauth_token_secret = Rails.application.secrets.twitter_oauth_token_secret
end
The secrets.yml will be checked into git by default, add it to your .gitignore file.
This is how I run rails console command:
COMPANY=b2b RAILS_ENV=development DEPLOY_ENV=localhost rails console
Instead I want to run only rails console command by setting these variables internally. How do I do that?
I saw some answers and made the config/company.yml file:
development:
COMPANY: b2b
DEPLOY_ENV: localhost
production:
COMPANY: b2c
DEPLOY_ENV: xyz.com
And I wrote the following config/initializers/company.rb file:
COMPANY_CONFIG = YAML.load_file("#{::Rails.root}/config/company.yml")[::Rails.env]
How do I access these variables in some helper/controller? I didn't understand how to do that? How do I set these ENV variables after running only rails console or rails server without passing the extra environment variable shown above?
You can't set environment variables in yaml file this difference things. Use dotenv gem for manage environment variables.
Read first Environment Variables
But have dirty hack. You can create ENV variable when rails loaded
add this code to before_configuration in config/application.rb:
config.before_configuration do
env_file = File.join(Rails.root, 'config', 'company.yml') # Find yml file
YAML.load(File.open(env_file)).each do |key, value| # Open file
ENV[key.to_s] = value # Set value to ENV
end if File.exists?(env_file) # If file exists
end
And now you can access to env variables from yml file anywhere in app.
I do not test this but i think this work.
This work.
Some info
You can create these settings variables in the config/initializers/application.rb file:
company_settings_path = Rails.root.join('config', 'company.yml')
COMPANY_CONFIG = { development: { company: :default, deploy_env: :none_yet } }
COMPANY_CONFIG = YAML::load(File.open(company_settings_path)) if File.exists?(company_settings_path)
COMPANY_CONFIG = COMPANY_CONFIG.with_indifferent_access
And then access them like this:
# controller
def show
if COMPANY_CONFIG[:company] == 'b2c'
# your logic
else
# other logic
end
end
You can acces the constant COMPANY_CONFIG everywhere in your app (models, controllers, views, helpers) the same way I used below.
I've been trying to figure out how Ryan Bates, in his Facebook Authentication screencast, is setting the following "FACEBOOK_APP_ID" and "FACEBOOK_SECRET" environment variables.
provider :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_SECRET']
There are similar-ish questions around, but no answers that I've been able to get to work on Rails 3.2.1.
UPDATE:
As of May 2013, my preferred way to handle ENV variables is via the Figaro gem
You could take a look at the comments:
You can either set environment variables directly on the shell where you are starting your server:
FACEBOOK_APP_ID=12345 FACEBOOK_SECRET=abcdef rails server
Or (rather hacky), you could set them in your config/environments/development.rb:
ENV['FACEBOOK_APP_ID'] = "12345";
ENV['FACEBOOK_SECRET'] = "abcdef";
An alternative way
However I would do neither. I would create a config file (say config/facebook.yml) which holds the corresponding values for every environment. And then load this as a constant in an initializer:
config/facebook.yml
development:
app_id: 12345
secret: abcdef
test:
app_id: 12345
secret: abcdef
production:
app_id: 23456
secret: bcdefg
config/initializers/facebook.rb
FACEBOOK_CONFIG = YAML.load_file("#{::Rails.root}/config/facebook.yml")[::Rails.env]
Then replace ENV['FACEBOOK_APP_ID'] in your code by FACEBOOK_CONFIG['app_id'] and ENV['FACEBOOK_SECRET'] by FACEBOOK_CONFIG['secret'].
There are several options:
Set the environment variables from the command line:
export FACEBOOK_APP_ID=your_app_id
export FACEBOOK_SECRET=your_secret
You can put the above lines in your ~/.bashrc
Set the environment variables when running rails s:
FACEBOOK_APP_ID=your_app_id FACEBOOK_SECRET=your_secret rails s
Create a .env file with:
FACEBOOK_APP_ID=your_app_id
FACEBOOK_SECRET=your_secret
and use either Foreman (starting your app with foreman start) or the dotenv gem.
Here's another idea. Define the keys and values in provider.yml file, as suggested above. Then put this in your environment.rb (before the call to Application.initialize!):
YAML.load_file("#{::Rails.root}/config/provider.yml")[::Rails.env].each {|k,v| ENV[k] = v }
Then these environment variables can be referenced in the omniauth initializer without any ordering dependency among intializers.
I'm trying to get my refinery cms image storage to Amazon s3 and I'm following this guide:
http://refinerycms.com/guides/how-to-use-amazon-s3-for-storage
But I'm blocked here:
There are a number of ways to set
these with your credentials, including
unix variables or settings them
manually through Ruby using ENV.
How do I define these credentials. Do I put something like :S3_KEY =>
"my_key" in my environments.rb file? I tried this and it didn't work.
I also tried this:
AWS::S3::Base.establish_connection!(
:access_key_id => ENV['S3_KEY'] || 'key_goes_here',
:secret_access_key => ENV['S3_SECRET'] || 's3_secret_key_here',
)
Can't figure out how to do this. Any ideas are greatly appreciated.
The safest way is to specify them as environment variables, so they aren't included in your source code. If you're the only one with access to the source, then specifying them as you describe should work.
You can specify them in your ~/.bashrc
export S3_KEY=mykey
export S3_SECRET=mysecret
Or if you're just testing locally you can prepend them to your rails command.
$ S3_KEY=mykey S3_SECRET=mysecret rails server
If you don't want to/can't use environment variables, another method is to use an initializer to load credentials from a yml file: config/initializers/s3_credentials.rb
# Load AWS::S3 configuration values
#
S3_CREDENTIALS = \
YAML.load_file(File.join(Rails.root, 'config/s3_credentials.yml'))[Rails.env]
# Set the AWS::S3 configuration
#
AWS::S3::Base.establish_connection! S3_CREDENTIALS['connection']
config/s3_credentials.yml
development: &defaults
connection:
:access_key_id: AAAAAA_your-key-here
:secret_access_key: 4rpsi235js_your-secret-here
:use_ssl: true
bucket: project-development
acl: public-read
production:
<<: *defaults
bucket: project