gpt4 book ai didi

ruby-on-rails - 使用 .env 配置运行 Rails 控制台

转载 作者:太空宇宙 更新时间:2023-11-03 16:16:30 25 4
gpt4 key购买 nike

我有一个部署到 Heroku 并与 S3 对话的 Rails 4 应用程序。 S3 的访问凭据存储在 Heroku 上的 ENV 变量中,我在本地有一个 .env 文件。

当我通过 heroku local -p 3000 在本地运行服务器时,一切正常 - 文件上传到 S3 就好了。

当我尝试通过 heroku local:run rails console 运行 rails console 时,我找不到避免 key 未找到错误的神奇方法。

我错过了什么?

.环境

S3_BUCKET_NAME=BUCKETNAME
AWS_ACCESS_KEY_ID=KEYID
AWS_SECRET_ACCESS_KEY=SECRETKEY
AWS_REGION=REGION

简介

web: bundle exec rails server -p $PORT

配置/环境/开发.rb

...
config.paperclip_defaults = {
storage: :s3,
s3_credentials: {
bucket: ENV.fetch('S3_BUCKET_NAME'),
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
s3_region: ENV.fetch('AWS_REGION'),
}
}

错误堆栈:

heroku local:run rails console
[OKAY] Loaded ENV .env File as KEY=VALUE Format
/Users/username/Development/rails_application/config/environments/development.rb:48:in `fetch': key not found: "S3_BUCKET_NAME" (KeyError)
from /Users/username/Development/rails_application/config/environments/development.rb:48:in `block in <top (required)>'
from /Users/username/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/railtie.rb:210:in `instance_eval'
from /Users/username/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/railtie.rb:210:in `configure'
from /Users/username/Development/rails_application/config/environments/development.rb:1:in `<top (required)>'
from /Users/username/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `require'
from /Users/username/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `block in require'
from /Users/username/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/username/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `require'
from /Users/username/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/engine.rb:598:in `block (2 levels) in <class:Engine>'
from /Users/username/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/engine.rb:597:in `each'
from /Users/username/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/engine.rb:597:in `block in <class:Engine>'
from /Users/username/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/initializable.rb:30:in `instance_exec'
from /Users/username/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/initializable.rb:30:in `run'
from /Users/username/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/initializable.rb:55:in `block in run_initializers'
from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:226:in `block in tsort_each'
from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:420:in `block (2 levels) in each_strongly_connected_component_from'
from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:429:in `each_strongly_connected_component_from'
from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:419:in `block in each_strongly_connected_component_from'
from /Users/username/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/initializable.rb:44:in `each'
from /Users/username/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/initializable.rb:44:in `tsort_each_child'
from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:413:in `call'
from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:413:in `each_strongly_connected_component_from'
from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:347:in `block in each_strongly_connected_component'
from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:345:in `each'
from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:345:in `call'
from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:345:in `each_strongly_connected_component'
from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:224:in `tsort_each'
from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:203:in `tsort_each'
from /Users/username/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/initializable.rb:54:in `run_initializers'
from /Users/username/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/application.rb:352:in `initialize!'
from /Users/username/Development/rails_application/config/environment.rb:5:in `<top (required)>'
from /Users/username/.rvm/gems/ruby-2.2.1/gems/spring-2.0.1/lib/spring/application.rb:92:in `require'
from /Users/username/.rvm/gems/ruby-2.2.1/gems/spring-2.0.1/lib/spring/application.rb:92:in `preload'
from /Users/username/.rvm/gems/ruby-2.2.1/gems/spring-2.0.1/lib/spring/application.rb:143:in `serve'
from /Users/username/.rvm/gems/ruby-2.2.1/gems/spring-2.0.1/lib/spring/application.rb:131:in `block in run'
from /Users/username/.rvm/gems/ruby-2.2.1/gems/spring-2.0.1/lib/spring/application.rb:125:in `loop'
from /Users/username/.rvm/gems/ruby-2.2.1/gems/spring-2.0.1/lib/spring/application.rb:125:in `run'
from /Users/username/.rvm/gems/ruby-2.2.1/gems/spring-2.0.1/lib/spring/application/boot.rb:19:in `<top (required)>'
from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'

最佳答案

来自 dotenv 自述文件

# config/application.rb
Bundler.require(*Rails.groups)

Dotenv::Railtie.load

HOSTNAME = ENV['HOSTNAME']

也看这里.env not being loaded in test environment in Rails with rspec

关于ruby-on-rails - 使用 .env 配置运行 Rails 控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42887247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com