如何将数据库分配给rails项目?

时间:2016-03-23 16:37:21

标签: ruby-on-rails

最近我从github克隆了一个rails项目。我已经有了一个数据库所以我认为没关系,但每当我在我的rails项目中运行rails服务器时,我都会收到此错误。

我注意到也没有Database.yml文件。

我的rails项目名称是anotiv

我的数据库名称是anotiv_development。

这是错误 -

All commands can be run with -h (or --help) for more information.
clearloadsave@clearloadsave:~/anotiv$ rails s
=> Booting Thin
=> Rails 4.2.3 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
/home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/railties-4.2.3/lib/rails/application/configuration.rb:110:in `database_configuration': Cannot load `Rails.application.database_configuration`: (RuntimeError)
Could not load database configuration. No such file - ["config/database.yml"]
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/railtie.rb:117:in `block (2 levels) in <class:Railtie>'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/activesupport-4.2.3/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/activesupport-4.2.3/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/activesupport-4.2.3/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/activesupport-4.2.3/lib/active_support/lazy_load_hooks.rb:27:in `each'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/activesupport-4.2.3/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/railtie.rb:116:in `block in <class:Railtie>'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/railties-4.2.3/lib/rails/initializable.rb:30:in `instance_exec'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/railties-4.2.3/lib/rails/initializable.rb:30:in `run'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/railties-4.2.3/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/2.0.0/tsort.rb:150:in `block in tsort_each'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/2.0.0/tsort.rb:183:in `block (2 levels) in each_strongly_connected_component'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/2.0.0/tsort.rb:219:in `each_strongly_connected_component_from'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/2.0.0/tsort.rb:182:in `block in each_strongly_connected_component'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/2.0.0/tsort.rb:180:in `each'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/2.0.0/tsort.rb:180:in `each_strongly_connected_component'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/2.0.0/tsort.rb:148:in `tsort_each'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/railties-4.2.3/lib/rails/initializable.rb:54:in `run_initializers'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/railties-4.2.3/lib/rails/application.rb:352:in `initialize!'
    from /home/clearloadsave/anotiv/config/environment.rb:5:in `<top (required)>'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in `require'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in `block in require'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in `require'
    from /home/clearloadsave/anotiv/config.ru:3:in `block in <main>'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/rack-1.6.4/lib/rack/builder.rb:55:in `instance_eval'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/rack-1.6.4/lib/rack/builder.rb:55:in `initialize'
    from /home/clearloadsave/anotiv/config.ru:in `new'
    from /home/clearloadsave/anotiv/config.ru:in `<main>'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/rack-1.6.4/lib/rack/builder.rb:49:in `eval'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/rack-1.6.4/lib/rack/builder.rb:49:in `new_from_string'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/rack-1.6.4/lib/rack/builder.rb:40:in `parse_file'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/rack-1.6.4/lib/rack/server.rb:299:in `build_app_and_options_from_config'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/rack-1.6.4/lib/rack/server.rb:208:in `app'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/railties-4.2.3/lib/rails/commands/server.rb:61:in `app'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/rack-1.6.4/lib/rack/server.rb:336:in `wrapped_app'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/railties-4.2.3/lib/rails/commands/server.rb:139:in `log_to_stdout'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/railties-4.2.3/lib/rails/commands/server.rb:78:in `start'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:80:in `block in server'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:75:in `tap'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:75:in `server'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /home/clearloadsave/.rbenv/versions/2.0.0-p643/lib/ruby/gems/2.0.0/gems/railties-4.2.3/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

3 个答案:

答案 0 :(得分:1)

在开源项目中,通常不包含database.yml文件b / c它具有用于访问数据库的密码(对于每个环境)。所以你应该创建一个。

例如,您可以创建一个新的rails项目,然后将database.yml文件(在项目的config目录中)复制到现有项目中。创建新项目时,可以指定应使用的数据库:

rails new my_project -d mysql

从文档中您可以指定的数据库是:

mysql,oracle,postgresql,sqlite3,frontbase,ibm_db,sqlserver,jdbcmysql,jdbcsqlite3,jdbcpostgresql,jdbc

答案 1 :(得分:0)

Could not load database configuration. No such file - ["config/database.yml"]

这是一个非常明确的错误消息,按照模式nunjucks创建文件。

答案 2 :(得分:0)

一旦你有了rails项目并且你也安装并运行了一个数据库系统(我推荐了令人惊奇的postgresql,但你觉得任何数据库都很好),只需让你的rails应用程序知道你要连接到它。您需要提供用户名和密码。

对于PostgreSQL:

在gemfile中:

gem 'pg'

然后安装gem:

bundle install

然后从你的rails app文件夹中执行:

touch config/database.yml

这是一个YAML文件,您可以将连接详细信息提供给。因此,打开文本编辑器并输入以下内容:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  timeout: 5000
  username: YOURUSERNAME
  password: YOURPASSWORD
  host: localhost
  port: 5432

development:
  <<: *default
  database: YOURAPPNAME_development

test:
  <<: *default
  database: YOURAPPNAME_test

production:
  <<: *default
  database: YOURAPPNAME

最后,只需使用rake创建新的空数据库。

rake db:create

这可以让你启动你的服务器:

rails s