How to get global variables from database config file in rails application?How to list the tables in a SQLite database file that was opened with ATTACH?How can I rename a database column in a Ruby on Rails migration?How do I get the current absolute URL in Ruby on Rails?Problem when I run my server on localhost (ruby on rails)MySql Error 1045 after rake db:createrails can not connect to the globale database server even host name and database are givenInstalling mysql with brewRoR 4 ERROR on 'rake db:create' or 'rake about'Gem::LoadError Specified 'mysql2' for database adapter, but the gem is not loadedwhy is Rake aborting because of “mysql” on windows
Quick destruction of a helium filled airship?
How do I answer an interview question about how to handle a hard deadline I won't be able to meet?
Why is the battery jumpered to a resistor in this schematic?
Earliest evidence of objects intended for future archaeologists?
Why should P.I be willing to write strong LOR even if that means losing a undergraduate from his/her lab?
Representing an indicator function: binary variables and "indicator constraints"
Has there ever been a truly bilingual country prior to the contemporary period?
Vegetarian dishes on Russian trains (European part)
What should I do with the stock I own if I anticipate there will be a recession?
Parse a simple key=value config file in C
How could Tony Stark wield the Infinity Nano Gauntlet - at all?
Why do aircraft leave cruising altitude long before landing just to circle?
How to train a replacement without them knowing?
Build a mob of suspiciously happy lenny faces ( ͡° ͜ʖ ͡°)
Have made several mistakes during the course of my PhD. Can't help but feel resentment. Can I get some advice about how to move forward?
How to use the passive form to say "This flower was watered."
From where do electrons gain kinetic energy through a circuit?
The Lucky House
Can planar set contain even many vertices of every unit equilateral triangle?
Meaning and structure of headline "Hair it is: A List of ..."
Postdoc interview - somewhat positive reply but no news?
Polar contour plot in Mathematica?
Reducing contention in thread-safe LruCache
Why does this image of cyclocarbon look like a nonagon?
How to get global variables from database config file in rails application?
How to list the tables in a SQLite database file that was opened with ATTACH?How can I rename a database column in a Ruby on Rails migration?How do I get the current absolute URL in Ruby on Rails?Problem when I run my server on localhost (ruby on rails)MySql Error 1045 after rake db:createrails can not connect to the globale database server even host name and database are givenInstalling mysql with brewRoR 4 ERROR on 'rake db:create' or 'rake about'Gem::LoadError Specified 'mysql2' for database adapter, but the gem is not loadedwhy is Rake aborting because of “mysql” on windows
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I want to get secret informations from other place beside rails application and the host. I made a global variable file under config/initializers
to get the keys. Then set them in the database connection file. Like this:
config/initializers/get_secrets.rb
@@username = 'A'
@@password = 'B'
config/database.yml
development:
adapter: mysql2
encoding: utf8
database: my_db_name
username: <%= @@username %>
password: <%= @@password %>
host: 127.0.0.1
port: 3306
When ran rails console
, got:
./config/initializers/get_secrets.rb:1: warning: class variable access from toplevel
(erb):10: warning: class variable access from toplevel
>
It seems not a good usage in this case. Then is there a right way?
ruby-on-rails database
add a comment |
I want to get secret informations from other place beside rails application and the host. I made a global variable file under config/initializers
to get the keys. Then set them in the database connection file. Like this:
config/initializers/get_secrets.rb
@@username = 'A'
@@password = 'B'
config/database.yml
development:
adapter: mysql2
encoding: utf8
database: my_db_name
username: <%= @@username %>
password: <%= @@password %>
host: 127.0.0.1
port: 3306
When ran rails console
, got:
./config/initializers/get_secrets.rb:1: warning: class variable access from toplevel
(erb):10: warning: class variable access from toplevel
>
It seems not a good usage in this case. Then is there a right way?
ruby-on-rails database
1
Why are you declaring database username and password globally? You should use environement variable insteadENV[:username], ENV[:password]
– ashvin
Mar 27 at 13:46
That’s the special needs. Don’t save those informations locally.
– Jingqiang Zhang
Mar 27 at 14:18
add a comment |
I want to get secret informations from other place beside rails application and the host. I made a global variable file under config/initializers
to get the keys. Then set them in the database connection file. Like this:
config/initializers/get_secrets.rb
@@username = 'A'
@@password = 'B'
config/database.yml
development:
adapter: mysql2
encoding: utf8
database: my_db_name
username: <%= @@username %>
password: <%= @@password %>
host: 127.0.0.1
port: 3306
When ran rails console
, got:
./config/initializers/get_secrets.rb:1: warning: class variable access from toplevel
(erb):10: warning: class variable access from toplevel
>
It seems not a good usage in this case. Then is there a right way?
ruby-on-rails database
I want to get secret informations from other place beside rails application and the host. I made a global variable file under config/initializers
to get the keys. Then set them in the database connection file. Like this:
config/initializers/get_secrets.rb
@@username = 'A'
@@password = 'B'
config/database.yml
development:
adapter: mysql2
encoding: utf8
database: my_db_name
username: <%= @@username %>
password: <%= @@password %>
host: 127.0.0.1
port: 3306
When ran rails console
, got:
./config/initializers/get_secrets.rb:1: warning: class variable access from toplevel
(erb):10: warning: class variable access from toplevel
>
It seems not a good usage in this case. Then is there a right way?
ruby-on-rails database
ruby-on-rails database
asked Mar 27 at 13:33
Jingqiang ZhangJingqiang Zhang
6387 silver badges20 bronze badges
6387 silver badges20 bronze badges
1
Why are you declaring database username and password globally? You should use environement variable insteadENV[:username], ENV[:password]
– ashvin
Mar 27 at 13:46
That’s the special needs. Don’t save those informations locally.
– Jingqiang Zhang
Mar 27 at 14:18
add a comment |
1
Why are you declaring database username and password globally? You should use environement variable insteadENV[:username], ENV[:password]
– ashvin
Mar 27 at 13:46
That’s the special needs. Don’t save those informations locally.
– Jingqiang Zhang
Mar 27 at 14:18
1
1
Why are you declaring database username and password globally? You should use environement variable instead
ENV[:username], ENV[:password]
– ashvin
Mar 27 at 13:46
Why are you declaring database username and password globally? You should use environement variable instead
ENV[:username], ENV[:password]
– ashvin
Mar 27 at 13:46
That’s the special needs. Don’t save those informations locally.
– Jingqiang Zhang
Mar 27 at 14:18
That’s the special needs. Don’t save those informations locally.
– Jingqiang Zhang
Mar 27 at 14:18
add a comment |
1 Answer
1
active
oldest
votes
In Rails 5.2+, we have a master.key file and a credentials YAML file. The master.key file encrypts your credentials (so never show anyone this key except your server).
You can activate this by doing:
EDITOR=vim rails credentials:edit
The file:
aws:
key: 123
google_key: 456
secret_key_base: f230ffddd1d1f151j3bchjas9a292j221...
If you want to use a credential, it looks like this:
Rails.application.credentials.google_key
or for nested resources like the AWS one:
Rails.application.credentials.aws[:key]
But if you're on Rails 5.1 or less, you don't have this ability. So, you do it in a different way:
Environment variables. Your program environment can always export an ENV and retrieve it. I always do this for simple nuisances like test email passwords:
export GMAIL_PASSWORD="1234"
and its usage,ENV['GMAIL_PASSWORD']
Secrets.yml. This is a file that basically predates master.key + credentials by functioning as an in between credential handler for your environments. It's janky and often erroneously committed to repos by accident, but we do what we can:
development:
secret_key_base: 1112sdasae1d13d1tfd1d3...
GMAIL_PASSWORD: '1234'
test:
secret_key_base: _your_secret_ as above
production:
secret_key_base: <%= secure_token %>
And it is accessed like Rails.application.secrets.GMAIL_PASSWORD
If you cannot access your variables, it may be because the secrets.yml file is not being loading in or you are in the wrong environment. Notice those vars are nested in particular environments.
Thank you. I know this way. But now only want to get secret from remote service. Yes, not local but a third party service.
– Jingqiang Zhang
Mar 28 at 0:07
We changed idea and set them to env.
– Jingqiang Zhang
Mar 28 at 11:36
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55378494%2fhow-to-get-global-variables-from-database-config-file-in-rails-application%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In Rails 5.2+, we have a master.key file and a credentials YAML file. The master.key file encrypts your credentials (so never show anyone this key except your server).
You can activate this by doing:
EDITOR=vim rails credentials:edit
The file:
aws:
key: 123
google_key: 456
secret_key_base: f230ffddd1d1f151j3bchjas9a292j221...
If you want to use a credential, it looks like this:
Rails.application.credentials.google_key
or for nested resources like the AWS one:
Rails.application.credentials.aws[:key]
But if you're on Rails 5.1 or less, you don't have this ability. So, you do it in a different way:
Environment variables. Your program environment can always export an ENV and retrieve it. I always do this for simple nuisances like test email passwords:
export GMAIL_PASSWORD="1234"
and its usage,ENV['GMAIL_PASSWORD']
Secrets.yml. This is a file that basically predates master.key + credentials by functioning as an in between credential handler for your environments. It's janky and often erroneously committed to repos by accident, but we do what we can:
development:
secret_key_base: 1112sdasae1d13d1tfd1d3...
GMAIL_PASSWORD: '1234'
test:
secret_key_base: _your_secret_ as above
production:
secret_key_base: <%= secure_token %>
And it is accessed like Rails.application.secrets.GMAIL_PASSWORD
If you cannot access your variables, it may be because the secrets.yml file is not being loading in or you are in the wrong environment. Notice those vars are nested in particular environments.
Thank you. I know this way. But now only want to get secret from remote service. Yes, not local but a third party service.
– Jingqiang Zhang
Mar 28 at 0:07
We changed idea and set them to env.
– Jingqiang Zhang
Mar 28 at 11:36
add a comment |
In Rails 5.2+, we have a master.key file and a credentials YAML file. The master.key file encrypts your credentials (so never show anyone this key except your server).
You can activate this by doing:
EDITOR=vim rails credentials:edit
The file:
aws:
key: 123
google_key: 456
secret_key_base: f230ffddd1d1f151j3bchjas9a292j221...
If you want to use a credential, it looks like this:
Rails.application.credentials.google_key
or for nested resources like the AWS one:
Rails.application.credentials.aws[:key]
But if you're on Rails 5.1 or less, you don't have this ability. So, you do it in a different way:
Environment variables. Your program environment can always export an ENV and retrieve it. I always do this for simple nuisances like test email passwords:
export GMAIL_PASSWORD="1234"
and its usage,ENV['GMAIL_PASSWORD']
Secrets.yml. This is a file that basically predates master.key + credentials by functioning as an in between credential handler for your environments. It's janky and often erroneously committed to repos by accident, but we do what we can:
development:
secret_key_base: 1112sdasae1d13d1tfd1d3...
GMAIL_PASSWORD: '1234'
test:
secret_key_base: _your_secret_ as above
production:
secret_key_base: <%= secure_token %>
And it is accessed like Rails.application.secrets.GMAIL_PASSWORD
If you cannot access your variables, it may be because the secrets.yml file is not being loading in or you are in the wrong environment. Notice those vars are nested in particular environments.
Thank you. I know this way. But now only want to get secret from remote service. Yes, not local but a third party service.
– Jingqiang Zhang
Mar 28 at 0:07
We changed idea and set them to env.
– Jingqiang Zhang
Mar 28 at 11:36
add a comment |
In Rails 5.2+, we have a master.key file and a credentials YAML file. The master.key file encrypts your credentials (so never show anyone this key except your server).
You can activate this by doing:
EDITOR=vim rails credentials:edit
The file:
aws:
key: 123
google_key: 456
secret_key_base: f230ffddd1d1f151j3bchjas9a292j221...
If you want to use a credential, it looks like this:
Rails.application.credentials.google_key
or for nested resources like the AWS one:
Rails.application.credentials.aws[:key]
But if you're on Rails 5.1 or less, you don't have this ability. So, you do it in a different way:
Environment variables. Your program environment can always export an ENV and retrieve it. I always do this for simple nuisances like test email passwords:
export GMAIL_PASSWORD="1234"
and its usage,ENV['GMAIL_PASSWORD']
Secrets.yml. This is a file that basically predates master.key + credentials by functioning as an in between credential handler for your environments. It's janky and often erroneously committed to repos by accident, but we do what we can:
development:
secret_key_base: 1112sdasae1d13d1tfd1d3...
GMAIL_PASSWORD: '1234'
test:
secret_key_base: _your_secret_ as above
production:
secret_key_base: <%= secure_token %>
And it is accessed like Rails.application.secrets.GMAIL_PASSWORD
If you cannot access your variables, it may be because the secrets.yml file is not being loading in or you are in the wrong environment. Notice those vars are nested in particular environments.
In Rails 5.2+, we have a master.key file and a credentials YAML file. The master.key file encrypts your credentials (so never show anyone this key except your server).
You can activate this by doing:
EDITOR=vim rails credentials:edit
The file:
aws:
key: 123
google_key: 456
secret_key_base: f230ffddd1d1f151j3bchjas9a292j221...
If you want to use a credential, it looks like this:
Rails.application.credentials.google_key
or for nested resources like the AWS one:
Rails.application.credentials.aws[:key]
But if you're on Rails 5.1 or less, you don't have this ability. So, you do it in a different way:
Environment variables. Your program environment can always export an ENV and retrieve it. I always do this for simple nuisances like test email passwords:
export GMAIL_PASSWORD="1234"
and its usage,ENV['GMAIL_PASSWORD']
Secrets.yml. This is a file that basically predates master.key + credentials by functioning as an in between credential handler for your environments. It's janky and often erroneously committed to repos by accident, but we do what we can:
development:
secret_key_base: 1112sdasae1d13d1tfd1d3...
GMAIL_PASSWORD: '1234'
test:
secret_key_base: _your_secret_ as above
production:
secret_key_base: <%= secure_token %>
And it is accessed like Rails.application.secrets.GMAIL_PASSWORD
If you cannot access your variables, it may be because the secrets.yml file is not being loading in or you are in the wrong environment. Notice those vars are nested in particular environments.
answered Mar 27 at 16:52
Veridian DynamicsVeridian Dynamics
1,1811 gold badge3 silver badges13 bronze badges
1,1811 gold badge3 silver badges13 bronze badges
Thank you. I know this way. But now only want to get secret from remote service. Yes, not local but a third party service.
– Jingqiang Zhang
Mar 28 at 0:07
We changed idea and set them to env.
– Jingqiang Zhang
Mar 28 at 11:36
add a comment |
Thank you. I know this way. But now only want to get secret from remote service. Yes, not local but a third party service.
– Jingqiang Zhang
Mar 28 at 0:07
We changed idea and set them to env.
– Jingqiang Zhang
Mar 28 at 11:36
Thank you. I know this way. But now only want to get secret from remote service. Yes, not local but a third party service.
– Jingqiang Zhang
Mar 28 at 0:07
Thank you. I know this way. But now only want to get secret from remote service. Yes, not local but a third party service.
– Jingqiang Zhang
Mar 28 at 0:07
We changed idea and set them to env.
– Jingqiang Zhang
Mar 28 at 11:36
We changed idea and set them to env.
– Jingqiang Zhang
Mar 28 at 11:36
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55378494%2fhow-to-get-global-variables-from-database-config-file-in-rails-application%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Why are you declaring database username and password globally? You should use environement variable instead
ENV[:username], ENV[:password]
– ashvin
Mar 27 at 13:46
That’s the special needs. Don’t save those informations locally.
– Jingqiang Zhang
Mar 27 at 14:18