I've written my own yaml config file as described in railscast #85. APP_CONFIG['FOO'] works in the initializers (e.g. sidekiq.yml), but not in database.yml (error: undefined method '+' for nil:NilClass; the '+' is being used in a concatenation: APP_CONFIG['FOO'] + 'bar').
Even putting APP_CONFIG into before_configuration did not solve this issue.
Rails.application.config.before_configuration do
APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")
end
Same problem. APP_CONFIG is still causing the nil:NilClass error in database.yml. So here's the question: how can i force my config.yml to be loaded before database.yml, so that it is available in database.yml as well as in the initializers (sidekiq.yml)?
Related
O configure my Ruby on Rails application to use sentry with error report, but it show-me this error:
URI::InvalidURIError:
bad URI(is not URI?): 'http://9ba0c50c55c94603a488a55516d5xxx:xxxx6d6468a4cb892140c1f86a9f228#sentry.myaddres.com/24'
When I remove 9ba0c50c55c94603a488a55516d5xxx:xxxx6d6468a4cb892140c1f86a9f228# this part of addresss all works fine, but in sentry documentation is:
Raven.configure do |config|
config.dsn = 'http://public:secret#example.com/project-id'
end
How can I solve this problem?
I was using ENV var to set sentry DSN:
# .env
SENTRY_DSN_URL='http://public:secret#example.com/project-id'
and in initializer
Raven.configure do |config|
config.dsn = ENV['SENTRY_DSN']
end
This problem is the quotation marks. To solve just remove them.
# .env
SENTRY_DSN_URL=http://public:secret#example.com/project-id
Works fine.
Rails 4 declares in config/secrets.yml constants secret_key_base for "verifying the integrity of signed cookies". Theses are 128 characters (0..f) long.
Paperclip (file management) can use :hash_secret option to encode accessibles file names.
https://github.com/thoughtbot/paperclip/wiki/Hashing
Is there a good idea to use secret_key_base as Paperclip hash ? It seems to be a good solution, because it is complexe enough, it's not in project's commits, and have one per environment.
Declare 2 variables in secrets.yml will looks like :
development:
secret_key_base: 73512
secret_key_asset: 123456
test:
secret_key_base: 3dde2
secret_key_asset: 789456
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
secret_key_asset: <%= ENV["SECRET_KEY_ASSET"] %>
... Seems to complicated for nothing for me.
Regards
According to this excerpt from the Paperclip Wiki it would appear that secret_key_base is fine.
# config/initializers/paperclip_defaults.rb
Paperclip::Attachment.default_options.update({
url: "/system/:class/:attachment/:id_partition/:style/:hash.:extension",
hash_secret: Rails.application.secrets.secret_key_base
})
You can use a different secret key for Paperclip, but it's probably unnecessary for most projects.
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 have following code in my deploy.rb
namespace :app do
desc "copies the configuration frile from ~/shared/config/*.yml to ~/config"
task :copy_config_files,:roles => :app do
run "cp -fv #{deploy_to}/shared/config/hoptoad.rb #{release_path}/config/initializers"
run "cp -fv #{deploy_to}/shared/config/app_config.yml #{release_path}/config/app_config.yml"
end
end
I thought it would be a good idea to keep my deploy.rb file clean and I attempted to move above code to capistrano_utilities.rb under config. I am using Rails application. And I added following line of code to deploy.rb
require File.expand_path(File.dirname(__FILE__) + "/../lib/capistrano_utilities")
Now I am getting following error.
undefined method `namespace' for main:Object (NoMethodError)
The value of self in the deploy.rb is Capistrano::Configuration . While the value of self in capistrano_utilities is Main. So I understand why I am getting namespace method error. What is the fix for this problem?
In your config/deploy.rb, try load instead of require. Also, capistrano already runs as if you're at the RAILS_ROOT, so there's no need to use __FILE__:
load "lib/capistrano_utilities"
In a capistrano config file, load is redefined to load another configuration file into the current configuration. When passing a path to it, it actually calls load_from_file (a private method defined by capistrano) that just reads the file from disk and instance_eval's it.
Check your Capfile on Rails.root.
if you use capistrano 3, you see this line;
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }
Now, put your file on "lib/capistrano/tasks/capistrano_utilities.cap" and it will be loaded.