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

前端 未结 3 1558
無奈伤痛
無奈伤痛 2021-02-10 09:19

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

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-10 10:14

    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.

提交回复
热议问题