如何使 rails 中的自定义环境成为默认环境? [英] How can I make a custom environment in rails a default environment?

查看:34
本文介绍了如何使 rails 中的自定义环境成为默认环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

i created 通过添加新文件 config/environments/staging.rb 在我的 rails 应用程序中创建了一个自定义暂存环境,与 config/environments/development 相同.rb然后添加数据库配置 config/database.yml

i created a custom staging environment in my rails app by adding new file config/environments/staging.rb, same as config/environments/development.rb and then added database config config/database.yml

staging:
  adapter: sqlite3
  database: db/staging.sqlite3
  pool: 5
  timeout: 5000

现在,我想让staging成为我的rails应用程序的默认环境,而不是开发.如何实现?

now, i want to make staging the default environment of my rails application instead of development.How to achieve it?

推荐答案

理想情况下,您必须像

  export RAILS_ENV=staging

因为 rails 完全依赖于环境变量.但就像你说的

because rails is fully dependent on environment variable. But like you said

在用户的 ~/.bashrc 或 ~/.bash_profile 文件中添加 RAILS_ENV.会使这个应用程序依赖于控制台,难道它不应该独立于 ~/.bashrc 或 ~/.bash_profile 文件工作吗?

adding RAILS_ENV in ~/.bashrc or ~/.bash_profile file of the user. will make this application depent on the console, shouldn't it just work independent of ~/.bashrc or ~/.bash_profile file?

显然,这是另一种选择.在 config/boot.rb

Obviously, this is another option. Include this line at the top of config/boot.rb

ENV["RACK_ENV"] = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || "staging"

这将适用于任何地方.我在以下地方测试过

This will work everywhere. I have tested in following places

  1. Rails 4
  2. 服务器
  3. 控制台
  4. dbconsole
  5. 如果在bashrczshrc等中设置,它会选择环境
  1. Rails 4
  2. Rake
  3. Server
  4. Console
  5. dbconsole
  6. It will pick the environment if it is set in bashrc or zshrc etc.

这篇关于如何使 rails 中的自定义环境成为默认环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆