Rails控制台默认环境

在我的开发机器上:

$ bundle exec rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Rails.env => "development" 

这是预料之中的。 到现在为止还挺好。

然而在我的生产服务器上(我使用Capistrano部署),我得到了完全相同的结果:

 $ bundle exec rails console Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Rails.env => "development" 

在任何一台机器上,我都可以这样做:

 $ bundle exec rails console production Loading development environment (Rails 3.2.3) 1.9.3p194 :001 > Rails.env => "production" 

我的问题是:在生产服务器上,不应该bundle exec rails console默认加载生产环境,而不是开发环境? 如果没有,为什么不呢?

rails可执行文件无法知道应在哪台机器上运行哪个环境。

您可以将export RAILS_ENV=production放在要启动控制台的用户的~/.bashrc~/.bash_profile文件中。

RAILS_ENV是一个与其他任何变量一样的变量,它总是默认为开发

如果你喜欢,你可以随时在生产服务器上打开’〜/ .bash_profile’并添加:

  alias sc="bundle exec rails console production" 

然后运行source ~/.bash_profile为你的终端会话重新加载该文件,你可以调用sc来加载控制台。