0
我用Devise,omniauth-facebook gem。如何在Rails的相同環境中通過域加載ENV變量?
我需要在相同的應用程序中爲同一環境(如生產)中的不同主機加載不同的ENV變量。需要實施Login with facebook
。
我有兩個主機相同的應用程序代碼:
1)www.abc-domain.com
2)www.xyz-domian.com
我的ENV變量(配置/設置.yml):
production:
abc-domain-credential:
facebook_access_key: "ABC_ACCESS_KEY"
facebook_api_key: "ABC_API_KEY"
xyz-domain-credential:
facebook_access_key: "XYZ_ACCESS_KEY"
facebook_api_key: "XYZ_API_KEY"
在我devise.rb文件(配置/初始化/ devise.rb):
Devise.setup do |config|
...
...
require "omniauth-facebook"
config.omniauth :facebook, ENV_SETTINGS["abc-domain-credential"]["facebook_access_key"], ENV_SETTINGS["abc-domain-credential"]["facebook_api_key"], scope: 'email', info_fields: 'email,name'
# ===== OR ======
config.omniauth :facebook, ENV_SETTINGS["xyz-domain-credential"]["facebook_access_key"], ENV_SETTINGS["xyz-domain-credential"]["facebook_api_key"], scope: 'email', info_fields: 'email,name'
...
...
end
當我訪問http://www.abc-domain.com然後在下面的env應該從settings.yml
abc-domain-credential:
facebook_access_key: "ABC_ACCESS_KEY"
facebook_api_key: "ABC_API_KEY"
負載當我訪問http://www.xyz-domain.com然後在下面的env應該是負荷settings.yml
xyz-domain-credential:
facebook_access_key: "XYZ_ACCESS_KEY"
facebook_api_key: "XYZ_API_KEY"
登錄與Facebook按鈕:
<%= link_to "https://stackoverflow.com/users/auth/facebook", "Login With Facebook" %>
什麼我要做的負載不同的ENV變量,對主機類似
if host == "abc-domain"
config.omniauth :facebook, ENV_SETTINGS["abc-domain-credential"]["facebook_access_key"], ENV_SETTINGS["abc-domain-credential"]["facebook_api_key"], scope: 'email', info_fields: 'email,name'
elsif host == "xyz-domain"
config.omniauth :facebook, ENV_SETTINGS["xyz-domain-credential"]["facebook_access_key"], ENV_SETTINGS["xyz-domain-credential"]["facebook_api_key"], scope: 'email', info_fields: 'email,name'
end
在config/initializer/devise.rb
文件
你在哪裏舉辦?你在使用類似heroku的東西嗎? – MZaragoza