rails database.yml 不接受 ERB [英] rails database.yml not accepting ERB

查看:24
本文介绍了rails database.yml 不接受 ERB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 database.yml 中,我有:

In my database.yml, I have:

staging:
  adapter: <%= ENV['DATABASE_ADAPTER'] %>
  encoding: <%= ENV['DATABASE_ENCODING'] %>
  database: <%= ENV['DATABASE'] %>
  host: <%= ENV['DATABASE_HOST'] %>
  port: <%= ENV['DATABASE_PORT'].to_i %>
  pool: <%= ENV['DATABASE_POOL'].to_i %>
  username: <%= ENV['DATABASE_USERNAME'] %>
  password: <%= ENV['DATABASE_PASSWORD'] %>

然而,它在实际启动 puma 时并没有读取 ERB 部分:

However, it does not read the ERB part when actually booting puma:

/usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in 
`require': Could not load 'active_record/connection_adapters/<%= ENV['DATABASE_ADAPTER'] %>_adapter'. 
Make sure that the adapter in config/database.yml is valid. If you use an adapter other than 'mysql', 'mysql2', 'postgresql' or 'sqlite3'
add the necessary adapter gem to the Gemfile. (LoadError)

这没有意义,因为在加载数据库配置的 Rails 代码中:

which makes no sense, since in the Rails code to load the database configuration:

  def database_configuration
    yaml = Pathname.new(paths["config/database"].existent.first || "")

    config = if yaml.exist?
      require "yaml"
      require "erb"
      YAML.load(ERB.new(yaml.read).result) || {}
    elsif ENV['DATABASE_URL']
      # Value from ENV['DATABASE_URL'] is set to default database connection
      # by Active Record.
      {}
    else
      raise "Could not load database configuration. No such file - #{yaml}"
    end

    config
  rescue Psych::SyntaxError => e
    raise "YAML syntax error occurred while parsing #{paths["config/database"].first}. " 
          "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " 
          "Error: #{e.message}"
  rescue => e
    raise e, "Cannot load `Rails.application.database_configuration`:
#{e.message}", e.backtrace
  end

(取自 Rails 4.2 稳定代码,我运行的是 4.2.1)

(taken from the Rails 4.2 stable code, I'm running 4.2.1)

我完全不明白为什么这不起作用,有什么想法吗?

I'm absolutely baffled why this isn't working, any ideas?

推荐答案

我刚刚经历了同样的事情,并且看到了您的帖子.我一直在关注一个教程,该教程让我创建了一个包含以下代码的 puma.conf 文件:

I just experienced the same thing, and came across your post. I had been following a tutorial that had me create a puma.conf file that contained the code below:

ActiveRecord::Base.establish_connection( YAML.load_file( "#{app_dir}/config/database.yml" )[rails_env])

我修改了以下内容,一切都按预期进行:

I modified to the following, and everything worked as expected:

require 'erb'
ActiveRecord::Base.establish_connection( YAML.load( ERB.new( File.read( "#{app_dir}/config/database.yml" )).result)[rails_env])

这篇关于rails database.yml 不接受 ERB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆