ror:Rails环境

Rails定义了三个环境:开发环境、测试环境、生产环境

控制台环境

Rails控制台默认使用的是开发环境,在启动控制台时可以看到:

➜  microblog git:(sign-up) rails c
... ...
Loading development environment (Rails 5.0.1)
2.3.3 :001 >

明确显示了“加载开发环境中...”,当然我们也可以使用Rails.env查看:

2.3.3 :001 > Rails.env
 => "development" 
2.3.3 :002 > Rails.env.development?
 => true 
2.3.3 :003 > Rails.env.test?
 => false 
2.3.3 :004 > Rails.env.production?
 => false 
2.3.3 :005 > 

如果我们想要使用其他环境的控制台,传递参数即可:

➜  microblog git:(sign-up) rails c test              
Running via Spring preloader in process 9121
Loading test environment (Rails 5.0.1)
2.3.3 :001 > 
➜  microblog git:(sign-up) rails c production        
Running via Spring preloader in process 9153
Loading production environment (Rails 5.0.1)
2.3.3 :001 > 
➜  microblog git:(sign-up) 

服务器环境

同样的,服务器环境默认也是开发环境,通过传递参数同样可以启动测试环境服务器和生产环境服务器:(由于hello_app不涉及数据库问题,因此这里使用hello_app做演示)

➜  hello_app git:(master) rails s --environment production
=> Booting Puma
=> Rails 5.0.1 application starting in production on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.6.2 (ruby 2.3.3-p222), codename: Sleepy Sunday Serenity
* Min threads: 5, max threads: 5
* Environment: production
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
... ...

➜  hello_app git:(master) rails s --environment test      
=> Booting Puma
=> Rails 5.0.1 application starting in test on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.6.2 (ruby 2.3.3-p222), codename: Sleepy Sunday Serenity
* Min threads: 5, max threads: 5
* Environment: test
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
... ...

➜  hello_app git:(master) 

Heroku环境

同样的道理可以查看Heroku的控制台环境,理所当然应该是生产环境

➜  microblog git:(sign-up) heroku run console
Running console on ⬢ fathomless-shelf-38262... up, run.4501 (Free)
Loading production environment (Rails 5.0.1)
irb(main):001:0> Rails.env
=> "production"
irb(main):002:0> Rails.env.production?
=> true
irb(main):003:0> 

你可能感兴趣的:(ror:Rails环境)