How can I make a custom environment in rails a default environment?

房东的猫 提交于 2019-12-04 13:33:12

问题


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

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


回答1:


Ideally you have to set environment variable in .bashrc like

  export RAILS_ENV=staging

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

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?

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. Rake
  3. Server
  4. Console
  5. dbconsole
  6. It will pick the environment if it is set in bashrc or zshrc etc.



回答2:


You can try this in your application.rb after Bundler.require statement

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



回答3:


I have put ENV['RAILS_ENV'] ||= 'custom_development' in boot.rb at the last. It worked.

I have tested

  • rake
  • server
  • console
  • dbconsole

All Worked.



来源:https://stackoverflow.com/questions/21674251/how-can-i-make-a-custom-environment-in-rails-a-default-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!