Rails应用程序连接到空数据库?

时间:2014-11-22 04:02:55

标签: ruby-on-rails

n00b问题。

基于我看到浏览器中加载的页面没有项目,我的rails应用程序似乎没有连接到数据库。

或至少,它似乎已连接到空数据库:

从rails控制台:

irb(main):004:0> Item.count
(0.3ms)  SELECT COUNT(*) FROM "items"
=> 0

这很奇怪,因为根据我在pgAdmin中的观点,我肯定有数据。

这是我的database.yml文件:

common: &common
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV['DB_POOL'] %>

development:
  <<: *common
  database: <%= ENV['DB_DEV_DATABASE'] %>
  username: <%= ENV['DB_DEV_USER'] %>
  password: <%= ENV['DB_DEV_PASSWORD'] %>
  host:     <%= ENV['DB_DEV_HOST'] %>
  port:     <%= ENV['DB_DEV_PORT'] %>

  # Schema search path. The server defaults to $user,public
  #schema_search_path: myapp,sharedapp,public

  # Minimum log levels, in increasing order:
  #   debug5, debug4, debug3, debug2, debug1,
  #   log, notice, warning, error, fatal, and panic
  # Defaults to warning.
  #min_messages: notice

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *common
  database: <%= ENV['DB_TEST_DATABASE'] %>
  username: <%= ENV['DB_TEST_USER'] %>
  password: <%= ENV['DB_TEST_PASSWORD'] %>
  host:     <%= ENV['DB_TEST_HOST'] %>
  port:     <%= ENV['DB_TEST_PORT'] %>

staging:
  <<: *common
  database: <%= ENV['DB_STAGING_DATABASE'] %>
  username: <%= ENV['DB_STAGING_USER'] %>
  password: <%= ENV['DB_STAGING_PASSWORD'] %>
  host:     <%= ENV['DB_STAGING_HOST'] %>
  port:     <%= ENV['DB_STAGING_PORT'] %>

production:
  <<: *common
  database: <%= ENV['DB_DATABASE'] %>
  username: <%= ENV['DB_USER'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  host:     <%= ENV['DB_HOST'] %>
  port:     <%= ENV['DB_PORT'] %>

1 个答案:

答案 0 :(得分:1)

我感觉您的环境变量(ENV['DB_DEV_DATABASE'])未正确设置。您可以根据shell环境(bash,zsh等)设置它们。例如,如果您在bash环境中工作,则需要将以下内容添加到~/.bashrc文件中。

export DB_DEV_DATABASE="yourapp_development"
# etc.

或者您可以放弃环境变量并直接在database.yml

中设置值
common: &common
  adapter: postgresql
  encoding: unicode

development:
  <<: *common
  database: yourapp_development
  username: username
  password: password
  host:     localhost
相关问题