开发环境的Ruby on Rails环境变量

时间:2022-06-01 18:28:54

Just a quick question to which I couldn't find an answer on *.

我有一个问题,我在*上找不到答案。

If it is easy to have environment variable for staging and production (on heroku for example), how can I set environment variable for my localhost (development environment)? (I am on a mac)

如果很容易为登台和生产设置环境变量(例如在heroku上),那么如何为本地主机(开发环境)设置环境变量?(我在mac上)

As of today I hardcode my api credential for development environment and I don't feel comfortable with that.

到今天为止,我硬编码了开发环境的api凭据,对此我感到不舒服。

Thanks !

谢谢!

3 个解决方案

#1


5  

Use dotenv is intended to be used in development:

使用dotenv是为了在开发中使用:

Add your application configuration to your .env file in the root of your project.

将应用程序配置添加到项目根目录中的.env文件中。

S3_BUCKET=YOURS3BUCKET
SECRET_KEY=YOURSECRETKEYGOESHERE

You may also add export in front of each line so you can source the file in bash. in .bashrc

您还可以在每行前面添加export,以便在bash中对文件进行源化。. bashrc中

export S3_BUCKET=YOURS3BUCKET
export SECRET_KEY=YOURSECRETKEYGOESHERE

Then access in rails app ENV['S3_BUCKET']

然后访问rails应用ENV['S3_BUCKET']

#2


1  

Environment variables are best placed in your .bash_profile file which lives in your home directory on the Mac: /Users/you/.bash_profile. Open that file and add something like this to the end of it:

环境变量最好放在您的.bash_profile文件中,该文件驻留在Mac上的主目录中:/Users/您/.bash_profile。打开该文件,在其末尾添加如下内容:

export MY_ENV_VAR=my_env_value

or

export MY_ENV_VAR="a string with spaces in it"

export is a shell command that sets environment variables. Your .bash_profile is a bash script that runs every time you open a new shell session (open a terminal window) and therefore your export commands will run and set the env vars.

export是设置环境变量的shell命令。您的.bash_profile是一个bash脚本,每次打开一个新的shell会话(打开一个终端窗口)时都会运行该脚本,因此您的导出命令将运行并设置env vars。

Then they will be available in the ENV constant when you're in Ruby.

然后,当您使用Ruby时,它们将在ENV常量中可用。

#3


1  

Edit /Users/your_user_name/.bash_profile and add there:

编辑/用户/ your_user_name /。bash_profile并添加:

export RAILS_ENV=development

#1


5  

Use dotenv is intended to be used in development:

使用dotenv是为了在开发中使用:

Add your application configuration to your .env file in the root of your project.

将应用程序配置添加到项目根目录中的.env文件中。

S3_BUCKET=YOURS3BUCKET
SECRET_KEY=YOURSECRETKEYGOESHERE

You may also add export in front of each line so you can source the file in bash. in .bashrc

您还可以在每行前面添加export,以便在bash中对文件进行源化。. bashrc中

export S3_BUCKET=YOURS3BUCKET
export SECRET_KEY=YOURSECRETKEYGOESHERE

Then access in rails app ENV['S3_BUCKET']

然后访问rails应用ENV['S3_BUCKET']

#2


1  

Environment variables are best placed in your .bash_profile file which lives in your home directory on the Mac: /Users/you/.bash_profile. Open that file and add something like this to the end of it:

环境变量最好放在您的.bash_profile文件中,该文件驻留在Mac上的主目录中:/Users/您/.bash_profile。打开该文件,在其末尾添加如下内容:

export MY_ENV_VAR=my_env_value

or

export MY_ENV_VAR="a string with spaces in it"

export is a shell command that sets environment variables. Your .bash_profile is a bash script that runs every time you open a new shell session (open a terminal window) and therefore your export commands will run and set the env vars.

export是设置环境变量的shell命令。您的.bash_profile是一个bash脚本,每次打开一个新的shell会话(打开一个终端窗口)时都会运行该脚本,因此您的导出命令将运行并设置env vars。

Then they will be available in the ENV constant when you're in Ruby.

然后,当您使用Ruby时,它们将在ENV常量中可用。

#3


1  

Edit /Users/your_user_name/.bash_profile and add there:

编辑/用户/ your_user_name /。bash_profile并添加:

export RAILS_ENV=development