0

我正在使用Heroku,并创建了一个新应用程序和一个 Postgres 空数据库设置。

我已经安装了 Heroku 工具带,可以登录 Heroku 并成功运行以下命令:

c:\>heroku pg:info mydbname --app my-app-name

c:\>heroku pg:credentials mydbname --app my-app-name

现在,我已经创建了我的应用程序和一个空数据库,我正在尝试成功连接到数据库。

我正在使用红宝石。我有我的项目的本地副本,我正在尝试创建一个简单的 ruby​​ 文件来连接到数据库。我只是想证明我可以从我的应用程序连接到数据库。

我在这里查看 activerecord 选项

但是,我运气不太好。

任何 heroku/postgres 大师都可以帮助我指出正确的方向。数据库为空。我要做的只是第一步成功连接到数据库。

红宝石文件:

require 'active_record'
require 'uri'

db = URI.parse(ENV['DATABASE_URL'] || 'postgres://localhost/jade')

ActiveRecord::Base.establish_connection(
  :adapter  => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
  :host     => db.host,
  :port     => db.port,
  :username => db.user,
  :password => db.password,
  :database => db.path[1..-1],
  :encoding => 'utf8'
)
4

1 回答 1

1

What is the content of ENV['DATABASE_URL']
Can you try it with enable user-env-compile

Making an app’s config vars present during the build allows for build-time optimizations to run in an environment that more closely resembles the runtime environment. This also allows config vars to be used to configure the build.

$ heroku labs:enable user-env-compile -a myapp
-----> Enabling user-env-compile for myapp... done
WARNING: This feature is experimental and may change or be removed without notice.

May help you missing-database-url

EDIT:

confirm that your database YAML file is being parsed properly.

ruby -ryaml -e "File.open('config/database.yml') { |f| puts YAML.load(f).inspect }"

Configuring a PostgreSQL Database

your config/database.yml customized to use PostgreSQL databases:

development:
  adapter: postgresql
  encoding: unicode
  database: MyDb
  pool: 5
  username: meuser
  password:

Another Tip
Look if you have a file called Gemfile in the root directory of your Rails app.
Look for gem 'pg' otherwise add it.

于 2013-01-08T14:32:58.360 回答