2016-02-24 23 views
1

我正在使用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' 
    … 

我在我的環境設置上面做錯了什麼?謝謝, - 戴夫

+0

如果保持ENV。應用程序本身的變量,你可能不會改變任何正確的?使用環境變量的主要原因是,如果您曾將此應用程序推入git,並且您的secret_key等其他信息向公衆顯示。如果你想將內容保存在應用程序中而不是存放在網站外,我建議至少將這些敏感文件添加到gitignore中。如果你與其他人合作,你可以使用「git update-index --assume-unchanged 」這將停止跟蹤更改,但不會從回購中刪除。只需在第一次提交後更改信息。 – bkunzi01

+0

是的,這個文件將會進入.gitignore,但是如何把這個文件放在.gitignore中來處理錯誤信息呢? – Dave

+0

嘗試'puts key.to_s;在ENV [key.to_s] = value'之前放置值並查看服務器停止的位置......它會告訴您導致錯誤的罪魁禍首變量......只是一個瘋狂的猜測,可能需要'value.to_s' – Vic

回答

1

我認爲這個錯誤並不是因爲你如何設置價值,而是因爲你設置的價值。

您的FACEBOOK_KEY值是數字,其中YAML.load_file將尊重。然後它會嘗試將這些數值存儲在ENV中。但是,ENV只喜歡字符串值。

2.2.1 :019 > ENV['test'] = 1 
TypeError: no implicit conversion of Fixnum into String 
from (irb):19:in `[]=' 
from (irb):19 

2.2.1 :020 > ENV['test'] = '1' 
=> "1" 

所以,如果你包裹在雙引號你FACEBOOK_KEY值,你應該沒問題:

development: 
    FACEBOOK_KEY: "1588888667329742" 
0

您可以使用寶石稱爲dotenv護欄。該文件是在這裏: https://github.com/bkeepers/dotenv 只需在Gemfile中寫:

gem 'dotenv-rails' 

然後運行bundle安裝。 之後,創建一個名爲「.env.local」應用程序的主目錄,並在該文件中寫入一個文件:

export GOOGLE_CLIENT_ID=YOUR_ID 

(ID沒有任何引號),您要使用只寫了一點:

ENV['GOOGLE_CLIENT_ID'] 

而對於生產只需創建另一個文件'.env.production'。 記得在gitignore中調用'.env *'。

對於研發和生產檢測的環境變量的更多信息,這個博客帖子:https://sulmanbaig.com/blogs/using-environment-variables-in-rails-heroku-capistrano

相關問題