ruby-on-rails - rails 凭据不获取

标签 ruby-on-rails ruby rubygems credentials

我通过 rails credentials:edit 命令创建了 rails config/master.key 和 config/credentials.yml.enc

在 Rails 控制台上我能够获取值但是当我运行 rake db:migrate 时,我得到这个错误:

invalid port number: "Rails.application.credentials.development[:port]"
Couldn't create 'Rails.application.credentials.development[:database]' database. Please check your configuration.
rake aborted!
PG::ConnectionBad: invalid port number: "Rails.application.credentials.development[:port]"
/home/ec2-user/.rvm/gems/ruby-2.6.1/gems/pg-1.1.4/lib/pg.rb:56:in `initialize'
/home/ec2-user/.rvm/gems/ruby-2.6.1/gems/pg-1.1.4/lib/pg.rb:56:in `new'
/home/ec2-user/.rvm/gems/ruby-2.6.1/gems/pg-1.1.4/lib/pg.rb:56:in `connect'
/home/ec2-user/.rvm/gems/ruby-2.6.1/gems/activerecord-5.2.3/lib/active_record/railties/databases.rake:29:in `block (2 levels) in <main>'
Tasks: TOP => db:create
(See full trace by running task with --trace)


#config/environments/development.rb
config.require_master_key = true

#config/credentials.yml.enc
development:
  database: xxxxxxxx
  username: yyyyyyyy
  password: zzzzzzzz
  host: aaaaaaa.com
  port: 1234

# Used as the base secret for all MessageVerifiers in Rails, including the one protecting cookies.
secret_key_base: 1234567890

#config/database.yml
default: &default
  adapter: postgresql
  encoding: utf8
  database: Rails.application.credentials.development[:database]
  username: Rails.application.credentials.development[:username]
  password: Rails.application.credentials.development[:password]
  host: Rails.application.credentials.development[:host]
  port: Rails.application.credentials.development[:port]

development:
  <<: *default

最佳答案

看起来是语法上的问题

你需要像这样使用插值:

  database: <%= Rails.application.credentials.development[:database] %>
  username: <%= Rails.application.credentials.development[:username] %>
  password: <%= Rails.application.credentials.development[:password] %>
  host: <%= Rails.application.credentials.development[:host] %>
  port: <%=Rails.application.credentials.development[:port] %>

关于ruby-on-rails - rails 凭据不获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57633182/

相关文章:

css - postcss 在需要包的 css 文件时找不到配置

ruby - 在 RSpec 中捕获 STDOUT

ruby-on-rails - 快速查看在当前范围内应用的变量以及对象自身所引用的变量的方法

Ruby 'tap' 方法 - 内部赋值

ruby - 使用关键字参数初始化 Ruby 结构

ruby-on-rails - 在 Windows 中设置 Curb

ruby - 无法在 rbenv 环境中切换 gem 版本

rubygems - 从 Github 分支安装 Gem?

ruby-on-rails - ActiveRecord::Errors 的 Rails 3 替代品是什么?

javascript - rails : Sprockets can't find file