我正在使用Rails 4.2.3並嘗試創建只能在我的應用程序中訪問的環境變量(而不是將它們存儲在〜/ .bashrc文件或某些特定於操作系統的解決方案中)。所以,我創建的文件「配置/ environment_variables.yml」與如何在Rails應用程序中設置環境變量?
development:
GOOGLE_CLIENT_ID: 999995268318-o5ejue1pgtsjoid0f0k8r7lcksfse6hk.apps.googleusercontent.com
GOOGLE_SECRET: 28bfoTU_RbRKkdxv7_wkNzw5
FACEBOOK_KEY: 1588888667329742
FACEBOOK_SECRET: 4444g1faeb11111e5392892d397b79f8e
production:
GOOGLE_CLIENT_ID: 999995268318-o5ejue1pgtsjoid0f0k8r7lcksfse6hk.apps.googleusercontent.com
GOOGLE_SECRET: 28bfoTU_RbRKkdxv7_wkNzw5
FACEBOOK_KEY: 1588888667329742
FACEBOOK_SECRET: 4444g1faeb11111e5392892d397b79f8e
,然後我創建的文件「配置/初始化/ environment_variables.rb」
module EnvironmentVariablesExample
class Application < Rails::Application
config.before_configuration do
env_file = Rails.root.join("config", 'environment_variables.yml').to_s
if File.exists?(env_file)
YAML.load_file(env_file)[Rails.env].each do |key, value|
ENV[key.to_s] = value
end # end YAML.load_file
end # end if File.exists?
end # end config.before_configuration
end # end class
end # end module
但是當我啓動我的服務器上我本地機器與「導軌s」,我得到這個錯誤
Exiting
/Users/davea/Documents/workspace/pushupmoose/config/initializers/environment_variables.rb:8:in `[]=': no implicit conversion of Fixnum into String (TypeError)
from /Users/davea/Documents/workspace/pushupmoose/config/initializers/environment_variables.rb:8:in `block (2 levels) in <class:Application>'
from /Users/davea/Documents/workspace/pushupmoose/config/initializers/environment_variables.rb:7:in `each'
from /Users/davea/Documents/workspace/pushupmoose/config/initializers/environment_variables.rb:7:in `block in <class:Application>'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/lazy_load_hooks.rb:27:in `each'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/railtie/configuration.rb:53:in `before_configuration'
from /Users/davea/Documents/workspace/pushupmoose/config/initializers/environment_variables.rb:3:in `<class:Application>'
from /Users/davea/Documents/workspace/pushupmoose/config/initializers/environment_variables.rb:2:in `<module:EnvironmentVariablesExample>'
from /Users/davea/Documents/workspace/pushupmoose/config/initializers/environment_variables.rb:1:in `<top (required)>'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `block in load'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/engine.rb:652:in `block in load_config_initializer'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/notifications.rb:166:in `instrument'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/engine.rb:651:in `load_config_initializer'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/engine.rb:616:in `block (2 levels) in <class:Engine>'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/engine.rb:615:in `each'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/engine.rb:615:in `block in <class:Engine>'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/initializable.rb:30:in `instance_exec'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/initializable.rb:30:in `run'
…
我在我的環境設置上面做錯了什麼?謝謝, - 戴夫
如果保持ENV。應用程序本身的變量,你可能不會改變任何正確的?使用環境變量的主要原因是,如果您曾將此應用程序推入git,並且您的secret_key等其他信息向公衆顯示。如果你想將內容保存在應用程序中而不是存放在網站外,我建議至少將這些敏感文件添加到gitignore中。如果你與其他人合作,你可以使用「git update-index --assume-unchanged」這將停止跟蹤更改,但不會從回購中刪除。只需在第一次提交後更改信息。 –
bkunzi01
是的,這個文件將會進入.gitignore,但是如何把這個文件放在.gitignore中來處理錯誤信息呢? – Dave
嘗試'puts key.to_s;在ENV [key.to_s] = value'之前放置值並查看服務器停止的位置......它會告訴您導致錯誤的罪魁禍首變量......只是一個瘋狂的猜測,可能需要'value.to_s' – Vic