我正在使用Rails 4.2.3并尝试创建仅在我的应用程序中可访问的环境变量(而不是将它们存储在〜/ .bashrc文件或某些特定于操作系统的解决方案中)。所以我用
创建了文件“config / environment_variables.yml”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
然后我创建了文件“config / initializers / 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
但是当我使用“rails 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 :(得分:1)
我认为错误不是因为您设置值的方式,而是因为您设置的值。
&#xA;&#xA;您的 FACEBOOK_KEY
值是 YAML.load_file
将遵循的数字。然后,它将尝试将这些数值存储在 ENV
中。但是, ENV
只喜欢字符串值。
2.2.1:019&gt; ENV ['test'] = 1&#xA; TypeError:没有将Fixnum隐式转换为String&#xA; from(irb):19:in [[] ='&#xA; from(irb):19&#xA;& #xA; 2.2.1:020&gt; ENV ['test'] ='1'&#xA; =&gt; “1”&#xA;
&#xA;&#xA; 因此,如果您将 FACEBOOK_KEY
值包装在双引号中,您应该没问题:< / p>&#XA;&#XA;
<代码>发展:&#XA; FACEBOOK_KEY:“1588888667329742”&#xA;&#xA;
答案 1 :(得分:1)
你可以使用名为dotenv-rails的gem。文档在这里: https://github.com/bkeepers/dotenv 只需在Gemfile中写:
gem 'dotenv-rails'
然后运行bundle install。 之后在app的主目录中创建一个名为'.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