I know that I can set my ENV variables in bash via
我知道我可以在bash via中设置我的ENV变量
export admin_password = "secret"
But is there a way to do it in my rails source code somewhere? My first attempt was something like this in environment/development.rb
但是在我的rails源代码中有什么方法可以实现吗?我的第一次尝试是在环境/开发方面
ENV['admin_password'] = "secret"
But it didn't work. Is there a way to do this?
但它不工作。有办法吗?
7 个解决方案
#1
67
[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.
您应该能够像您在问题中指定的那样设置环境变量。例如,我有一个Heroku应用程序,它使用HTTP基本身份验证。
# 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:
创建一个文件配置/应用程序。yml具有您希望能够访问的选项哈希:
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:
现在,创建文件配置/初始化器/app_config。并包括以下内容:
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.)
现在,在应用程序的任何地方,都可以访问APP_CONFIG[:admin_password]以及所有其他数据。注意,由于初始化器包含ERB。新的,您的YAML文件可以包含ERB标记。
#2
121
Never hardcode sensitive information (account credentials, passwords, etc.). Instead, create a file to store that information as environment variables (key/value pairs), and exclude that file from your source code management system. For example, in terms of Git (source code management system), exclude that file by adding it to .gitignore:
决不要硬编码敏感信息(帐户凭证、密码等)。相反,创建一个文件将该信息存储为环境变量(键/值对),并将该文件从源代码管理系统中排除。例如,在Git(源代码管理系统)中,将该文件添加到.gitignore中排除该文件:
-bash> echo '/config/app_environment_variables.rb' >> .gitignore
/config/app_environment_variables.rb
/ config / app_environment_variables.rb
ENV['HTTP_USER'] = 'devuser'
ENV['HTTP_PASS'] = 'devpass'
As well, add the following lines to /config/environment.rb
, between the require
line, and the Application.initialize
line:
同样,向/config/environment添加以下代码行。在需求行和应用程序之间的rb。初始化:
# Load the app's custom environment variables here, so that they are loaded before environments/*.rb
app_environment_variables = File.join(Rails.root, 'config', 'app_environment_variables.rb')
load(app_environment_variables) if File.exists?(app_environment_variables)
That's it!
就是这样!
As the comment above says, by doing this you will be loading your environment variables before environments/*.rb
, which means that you will be able to refer to your variables inside those files (e.g. environments/production.rb
). This is a great advantage over putting your environment variables file inside /config/initializers/
.
正如上面的注释所说,通过这样做,您将在environment /*之前加载环境变量。rb,这意味着您可以在这些文件中引用您的变量(例如environment /product .rb)。与将环境变量文件放在/config/initializers/中相比,这是一个很大的优势。
Inside app_environment_variables.rb
there's no need to distinguish environments as far as development or production because you will never commit this file into your source code management system, hence it is for the development context by default. But if you need to set something special for the test environment (or for occasions when you test production mode locally), just add a conditional block below all the other variables:
app_environment_variables内部。rb没有必要区分开发或生产环境,因为您永远不会将这个文件提交到源代码管理系统中,因此默认情况下它是用于开发上下文的。但是如果您需要为测试环境设置一些特殊的东西(或者当您在本地测试生产模式时),只需在所有其他变量下面添加一个条件块:
if Rails.env.test?
ENV['HTTP_USER'] = 'testuser'
ENV['HTTP_PASS'] = 'testpass'
end
if Rails.env.production?
ENV['HTTP_USER'] = 'produser'
ENV['HTTP_PASS'] = 'prodpass'
end
Whenever you update app_environment_variables.rb
, restart the app server. Assuming you are using the likes of Apache/Passenger or rails server
:
当你更新app_environment_variables。rb,重启app服务器。假设您使用的是Apache/Passenger或rails服务器:
-bash> touch tmp/restart.txt
In your code, refer to the environment variables as follows:
在您的代码中,参考以下环境变量:
def authenticate
authenticate_or_request_with_http_basic do |username, password|
username == ENV['HTTP_USER'] && password == ENV['HTTP_PASS']
end
end
Note that inside app_environment_variables.rb
you must specify booleans and numbers as strings (e.g. ENV['SEND_MAIL'] = 'false'
not just false
, and ENV['TIMEOUT'] = '30'
not just 30
), otherwise you will get the errors can't convert false into String
and can't convert Fixnum into String
, respectively.
注意,在app_environment_variables。rb必须将布尔值和数字指定为字符串(例如,ENV['SEND_MAIL']] = 'false'而不仅仅是false, ENV['TIMEOUT'] = '30'而不仅仅是30),否则您将得到错误不能将false转换为字符串,也不能将Fixnum分别转换为字符串。
Storing and sharing sensitive information
存储和共享敏感信息
The final knot to tie is: how to share this sensitive information with your clients and/or partners? For the purpose of business continuity (i.e. when you get hit by a falling star, how will your clients and/or partners resume full operations of the site?), your clients and/or partners need to know all the credentials required by your app. Emailing/Skyping these things around is insecure and leads to disarray. Storing it in shared Google Docs is not bad (if everyone uses https), but an app dedicated to storing and sharing small titbits like passwords would be ideal.
最后一个结是:如何与客户和/或合作伙伴分享这些敏感信息?为目的的业务连续性(即被流星击中时,你的客户和/或合作伙伴如何恢复全面运作的网站?),你的客户和/或合作伙伴需要知道应用程序所需的所有凭证,电子邮件/ Skyping这些东西是不安全的,会导致混乱。将它存储在共享的谷歌文档中并不坏(如果每个人都使用https的话),但是一个专门用于存储和共享密码等小标题的应用程序将是理想的。
How to set environment variables on Heroku
如何在Heroku上设置环境变量
If you have a single environment on Heroku:
如果Heroku上只有一个环境:
-bash> heroku config:add HTTP_USER='herouser'
-bash> heroku config:add HTTP_USER='heropass'
If you have multiple environments on Heroku:
如果你在Heroku上有多个环境:
-bash> heroku config:add HTTP_USER='staguser' --remote staging
-bash> heroku config:add HTTP_PASS='stagpass' --remote staging
-bash> heroku config:add HTTP_USER='produser' --remote production
-bash> heroku config:add HTTP_PASS='prodpass' --remote production
Foreman and .env
工头和.env
Many developers use Foreman (installed with the Heroku Toolbelt) to run their apps locally (as opposed to using the likes of Apache/Passenger or rails server
). Foreman and Heroku use Procfile
for declaring what commands are run by your application, so the transition from local dev to Heroku is seamless in that regard. I use Foreman and Heroku in every Rails project, so this convenience is great. But here's the thing.. Foreman loads environment variables stored in /.env
via dotenv but unfortunately dotenv essentially only parses the file for key=value
pairs; those pairs don't become variables right there and then, so you can't refer to already set variables (to keep things DRY), nor can you do "Ruby" in there (as noted above with the conditionals), which you can do in /config/app_environment_variables.rb
. For instance, in terms of keeping things DRY I sometimes do stuff like this:
许多开发人员使用Foreman(安装了Heroku工具带)在本地运行他们的应用程序(而不是使用Apache/乘客或rails服务器之类的工具)。Foreman和Heroku使用Procfile来声明应用程序运行的命令,因此从本地开发到Heroku的转换在这方面是无缝的。我在每个Rails项目中都使用了工头和Heroku,所以这非常方便。但是这里的东西. .福尔曼加载存储在/中的环境变量。env通过dotenv,但不幸的是dotenv只解析键=值对的文件;这些组合不会在这里成为变量,因此,您不能引用已经设置的变量(以使事情保持干燥),也不能在那里执行“Ruby”(如上所述的条件),您可以在/config/app_environment_variables.rb中执行。例如,为了保持干燥,我有时会这样做:
ENV['SUPPORT_EMAIL']='Company Support <support@company.com>'
ENV['MAILER_DEFAULT_FROM'] = ENV['SUPPORT_EMAIL']
ENV['MAILER_DEFAULT_TO'] = ENV['SUPPORT_EMAIL']
Hence, I use Foreman to run my apps locally, but I don't use its .env
file for loading environment variables; rather I use Foreman in conjunction with the /config/app_environment_variables.rb
approach described above.
因此,我使用Foreman在本地运行我的应用程序,但是我不使用它的.env文件来加载环境变量;相反,我将福尔曼与/config/app_environment_variables结合使用。rb上面描述的方法。
#3
8
As an aside to the solutions here, there are cleaner alternatives if you're using certain development servers.
除了解决方案之外,如果您使用某些开发服务器,还有更干净的替代方案。
With Heroku's Foreman, you can create per-project environment variables in a .env
file:
使用Heroku的工头,您可以在.env文件中创建每个项目的环境变量:
ADMIN_PASSOWRD="secret"
With Pow, you can use a .powenv
file:
使用Pow,您可以使用.powenv文件:
export ADMIN_PASSOWRD="secret"
#4
7
The way I am trying to do this in my question actually works!
我在我的问题中尝试的方法实际上是有效的!
# environment/development.rb
ENV['admin_password'] = "secret"
I just had to restart the server. I thought running reload!
in rails console would be enough but I also had to restart the web server.
我只需要重新启动服务器。我想重新加载运行!在rails控制台中,这已经足够了,但我还必须重新启动web服务器。
I am picking my own answer because I feel this is a better place to put and set the ENV variables
我选择了我自己的答案,因为我觉得这是放置和设置ENV变量的更好的地方
#5
2
I think the best way is to store them in some yml file and then load that file using this command in intializer file
我认为最好的方法是将它们存储在某个yml文件中,然后使用这个命令在初始化文件中加载该文件
APP_CONFIG = YAML.load_file("#{Rails.root}/config/CONFIG.yml")[Rails.env].to_hash
you can easily access environment related config variables.
您可以轻松访问与环境相关的配置变量。
Your Yml file key value structure:
你的Yml文件键值结构:
development:
app_key: 'abc'
app_secret: 'abc'
production:
app_key: 'xyz'
app_secret: 'ghq'
#6
1
The system environment and rails' environment are different things. ENV
let's you work with the rails' environment, but if what you want to do is to change the system's environment in runtime you can just surround the command with backticks.
系统环境和rails环境是不同的东西。ENV让我们来处理rails的环境,但是如果您想要做的是在运行时更改系统的环境,那么只需在命令周围加上反勾。
# ruby code
`export admin_password="secret"`
# more ruby code
#7
1
Script for loading of custom .env
file: Add the following lines to /config/environment.rb
, between the require
line, and the Application.initialize
line:
用于加载自定义.env文件的脚本:向/config/environment添加以下代码行。在需求行和应用程序之间的rb。初始化:
# Load the app's custom environment variables here, so that they are loaded before environments/*.rb
app_environment_variables = File.join(Rails.root, 'config', 'local_environment.env')
if File.exists?(app_environment_variables)
lines = File.readlines(app_environment_variables)
lines.each do |line|
line.chomp!
next if line.empty? or line[0] == '#'
parts = line.partition '='
raise "Wrong line: #{line} in #{app_environment_variables}" if parts.last.empty?
ENV[parts.first] = parts.last
end
end
And config/local_environment.env
(you will want to .gitignore
it) will look like:
和配置/ local_environment。env(你会想要。gitignore)会是:
# This is ignored comment
DATABASE_URL=mysql2://user:psw@0.0.0:3307/database
RACK_ENV=development
(Based on solution of @user664833)
(基于@user664833的解决方案)
#1
67
[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.
您应该能够像您在问题中指定的那样设置环境变量。例如,我有一个Heroku应用程序,它使用HTTP基本身份验证。
# 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:
创建一个文件配置/应用程序。yml具有您希望能够访问的选项哈希:
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:
现在,创建文件配置/初始化器/app_config。并包括以下内容:
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.)
现在,在应用程序的任何地方,都可以访问APP_CONFIG[:admin_password]以及所有其他数据。注意,由于初始化器包含ERB。新的,您的YAML文件可以包含ERB标记。
#2
121
Never hardcode sensitive information (account credentials, passwords, etc.). Instead, create a file to store that information as environment variables (key/value pairs), and exclude that file from your source code management system. For example, in terms of Git (source code management system), exclude that file by adding it to .gitignore:
决不要硬编码敏感信息(帐户凭证、密码等)。相反,创建一个文件将该信息存储为环境变量(键/值对),并将该文件从源代码管理系统中排除。例如,在Git(源代码管理系统)中,将该文件添加到.gitignore中排除该文件:
-bash> echo '/config/app_environment_variables.rb' >> .gitignore
/config/app_environment_variables.rb
/ config / app_environment_variables.rb
ENV['HTTP_USER'] = 'devuser'
ENV['HTTP_PASS'] = 'devpass'
As well, add the following lines to /config/environment.rb
, between the require
line, and the Application.initialize
line:
同样,向/config/environment添加以下代码行。在需求行和应用程序之间的rb。初始化:
# Load the app's custom environment variables here, so that they are loaded before environments/*.rb
app_environment_variables = File.join(Rails.root, 'config', 'app_environment_variables.rb')
load(app_environment_variables) if File.exists?(app_environment_variables)
That's it!
就是这样!
As the comment above says, by doing this you will be loading your environment variables before environments/*.rb
, which means that you will be able to refer to your variables inside those files (e.g. environments/production.rb
). This is a great advantage over putting your environment variables file inside /config/initializers/
.
正如上面的注释所说,通过这样做,您将在environment /*之前加载环境变量。rb,这意味着您可以在这些文件中引用您的变量(例如environment /product .rb)。与将环境变量文件放在/config/initializers/中相比,这是一个很大的优势。
Inside app_environment_variables.rb
there's no need to distinguish environments as far as development or production because you will never commit this file into your source code management system, hence it is for the development context by default. But if you need to set something special for the test environment (or for occasions when you test production mode locally), just add a conditional block below all the other variables:
app_environment_variables内部。rb没有必要区分开发或生产环境,因为您永远不会将这个文件提交到源代码管理系统中,因此默认情况下它是用于开发上下文的。但是如果您需要为测试环境设置一些特殊的东西(或者当您在本地测试生产模式时),只需在所有其他变量下面添加一个条件块:
if Rails.env.test?
ENV['HTTP_USER'] = 'testuser'
ENV['HTTP_PASS'] = 'testpass'
end
if Rails.env.production?
ENV['HTTP_USER'] = 'produser'
ENV['HTTP_PASS'] = 'prodpass'
end
Whenever you update app_environment_variables.rb
, restart the app server. Assuming you are using the likes of Apache/Passenger or rails server
:
当你更新app_environment_variables。rb,重启app服务器。假设您使用的是Apache/Passenger或rails服务器:
-bash> touch tmp/restart.txt
In your code, refer to the environment variables as follows:
在您的代码中,参考以下环境变量:
def authenticate
authenticate_or_request_with_http_basic do |username, password|
username == ENV['HTTP_USER'] && password == ENV['HTTP_PASS']
end
end
Note that inside app_environment_variables.rb
you must specify booleans and numbers as strings (e.g. ENV['SEND_MAIL'] = 'false'
not just false
, and ENV['TIMEOUT'] = '30'
not just 30
), otherwise you will get the errors can't convert false into String
and can't convert Fixnum into String
, respectively.
注意,在app_environment_variables。rb必须将布尔值和数字指定为字符串(例如,ENV['SEND_MAIL']] = 'false'而不仅仅是false, ENV['TIMEOUT'] = '30'而不仅仅是30),否则您将得到错误不能将false转换为字符串,也不能将Fixnum分别转换为字符串。
Storing and sharing sensitive information
存储和共享敏感信息
The final knot to tie is: how to share this sensitive information with your clients and/or partners? For the purpose of business continuity (i.e. when you get hit by a falling star, how will your clients and/or partners resume full operations of the site?), your clients and/or partners need to know all the credentials required by your app. Emailing/Skyping these things around is insecure and leads to disarray. Storing it in shared Google Docs is not bad (if everyone uses https), but an app dedicated to storing and sharing small titbits like passwords would be ideal.
最后一个结是:如何与客户和/或合作伙伴分享这些敏感信息?为目的的业务连续性(即被流星击中时,你的客户和/或合作伙伴如何恢复全面运作的网站?),你的客户和/或合作伙伴需要知道应用程序所需的所有凭证,电子邮件/ Skyping这些东西是不安全的,会导致混乱。将它存储在共享的谷歌文档中并不坏(如果每个人都使用https的话),但是一个专门用于存储和共享密码等小标题的应用程序将是理想的。
How to set environment variables on Heroku
如何在Heroku上设置环境变量
If you have a single environment on Heroku:
如果Heroku上只有一个环境:
-bash> heroku config:add HTTP_USER='herouser'
-bash> heroku config:add HTTP_USER='heropass'
If you have multiple environments on Heroku:
如果你在Heroku上有多个环境:
-bash> heroku config:add HTTP_USER='staguser' --remote staging
-bash> heroku config:add HTTP_PASS='stagpass' --remote staging
-bash> heroku config:add HTTP_USER='produser' --remote production
-bash> heroku config:add HTTP_PASS='prodpass' --remote production
Foreman and .env
工头和.env
Many developers use Foreman (installed with the Heroku Toolbelt) to run their apps locally (as opposed to using the likes of Apache/Passenger or rails server
). Foreman and Heroku use Procfile
for declaring what commands are run by your application, so the transition from local dev to Heroku is seamless in that regard. I use Foreman and Heroku in every Rails project, so this convenience is great. But here's the thing.. Foreman loads environment variables stored in /.env
via dotenv but unfortunately dotenv essentially only parses the file for key=value
pairs; those pairs don't become variables right there and then, so you can't refer to already set variables (to keep things DRY), nor can you do "Ruby" in there (as noted above with the conditionals), which you can do in /config/app_environment_variables.rb
. For instance, in terms of keeping things DRY I sometimes do stuff like this:
许多开发人员使用Foreman(安装了Heroku工具带)在本地运行他们的应用程序(而不是使用Apache/乘客或rails服务器之类的工具)。Foreman和Heroku使用Procfile来声明应用程序运行的命令,因此从本地开发到Heroku的转换在这方面是无缝的。我在每个Rails项目中都使用了工头和Heroku,所以这非常方便。但是这里的东西. .福尔曼加载存储在/中的环境变量。env通过dotenv,但不幸的是dotenv只解析键=值对的文件;这些组合不会在这里成为变量,因此,您不能引用已经设置的变量(以使事情保持干燥),也不能在那里执行“Ruby”(如上所述的条件),您可以在/config/app_environment_variables.rb中执行。例如,为了保持干燥,我有时会这样做:
ENV['SUPPORT_EMAIL']='Company Support <support@company.com>'
ENV['MAILER_DEFAULT_FROM'] = ENV['SUPPORT_EMAIL']
ENV['MAILER_DEFAULT_TO'] = ENV['SUPPORT_EMAIL']
Hence, I use Foreman to run my apps locally, but I don't use its .env
file for loading environment variables; rather I use Foreman in conjunction with the /config/app_environment_variables.rb
approach described above.
因此,我使用Foreman在本地运行我的应用程序,但是我不使用它的.env文件来加载环境变量;相反,我将福尔曼与/config/app_environment_variables结合使用。rb上面描述的方法。
#3
8
As an aside to the solutions here, there are cleaner alternatives if you're using certain development servers.
除了解决方案之外,如果您使用某些开发服务器,还有更干净的替代方案。
With Heroku's Foreman, you can create per-project environment variables in a .env
file:
使用Heroku的工头,您可以在.env文件中创建每个项目的环境变量:
ADMIN_PASSOWRD="secret"
With Pow, you can use a .powenv
file:
使用Pow,您可以使用.powenv文件:
export ADMIN_PASSOWRD="secret"
#4
7
The way I am trying to do this in my question actually works!
我在我的问题中尝试的方法实际上是有效的!
# environment/development.rb
ENV['admin_password'] = "secret"
I just had to restart the server. I thought running reload!
in rails console would be enough but I also had to restart the web server.
我只需要重新启动服务器。我想重新加载运行!在rails控制台中,这已经足够了,但我还必须重新启动web服务器。
I am picking my own answer because I feel this is a better place to put and set the ENV variables
我选择了我自己的答案,因为我觉得这是放置和设置ENV变量的更好的地方
#5
2
I think the best way is to store them in some yml file and then load that file using this command in intializer file
我认为最好的方法是将它们存储在某个yml文件中,然后使用这个命令在初始化文件中加载该文件
APP_CONFIG = YAML.load_file("#{Rails.root}/config/CONFIG.yml")[Rails.env].to_hash
you can easily access environment related config variables.
您可以轻松访问与环境相关的配置变量。
Your Yml file key value structure:
你的Yml文件键值结构:
development:
app_key: 'abc'
app_secret: 'abc'
production:
app_key: 'xyz'
app_secret: 'ghq'
#6
1
The system environment and rails' environment are different things. ENV
let's you work with the rails' environment, but if what you want to do is to change the system's environment in runtime you can just surround the command with backticks.
系统环境和rails环境是不同的东西。ENV让我们来处理rails的环境,但是如果您想要做的是在运行时更改系统的环境,那么只需在命令周围加上反勾。
# ruby code
`export admin_password="secret"`
# more ruby code
#7
1
Script for loading of custom .env
file: Add the following lines to /config/environment.rb
, between the require
line, and the Application.initialize
line:
用于加载自定义.env文件的脚本:向/config/environment添加以下代码行。在需求行和应用程序之间的rb。初始化:
# Load the app's custom environment variables here, so that they are loaded before environments/*.rb
app_environment_variables = File.join(Rails.root, 'config', 'local_environment.env')
if File.exists?(app_environment_variables)
lines = File.readlines(app_environment_variables)
lines.each do |line|
line.chomp!
next if line.empty? or line[0] == '#'
parts = line.partition '='
raise "Wrong line: #{line} in #{app_environment_variables}" if parts.last.empty?
ENV[parts.first] = parts.last
end
end
And config/local_environment.env
(you will want to .gitignore
it) will look like:
和配置/ local_environment。env(你会想要。gitignore)会是:
# This is ignored comment
DATABASE_URL=mysql2://user:psw@0.0.0:3307/database
RACK_ENV=development
(Based on solution of @user664833)
(基于@user664833的解决方案)