【问题标题】:rails generate model failedrails 生成模型失败
【发布时间】:2014-04-12 08:00:39
【问题描述】:

我正在学习 railstutorial.org 的教程,所以我决定使用 mysql 而不是 sqlite,但是当我输入终端时

rails 生成模型用户名:字符串邮箱:字符串

我收到此错误

    invoke  active_record
    /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/activerecord-3.0.12/lib/active_record/connection_adapters/abstract/connection_specification.rb:62:in `establish_connection': development database is not configured (ActiveRecord::AdapterNotSpecified)
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/activerecord-3.0.12/lib/active_record/connection_adapters/abstract/connection_specification.rb:55:in `establish_connection'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/activerecord-3.0.12/lib/active_record/railtie.rb:59:in `block (2 levels) in <class:Railtie>'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/activesupport-3.0.12/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/activesupport-3.0.12/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/activesupport-3.0.12/lib/active_support/lazy_load_hooks.rb:43:in `block in run_load_hooks'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/activesupport-3.0.12/lib/active_support/lazy_load_hooks.rb:42:in `each'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/activesupport-3.0.12/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/activerecord-3.0.12/lib/active_record/base.rb:1910:in `<top (required)>'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/railties-3.0.12/lib/rails/generators/named_base.rb:97:in `pluralize_table_names?'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/railties-3.0.12/lib/rails/generators/named_base.rb:44:in `table_name'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/activerecord-3.0.12/lib/rails/generators/active_record/model/model_generator.rb:16:in `create_migration_file'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/thor-0.14.6/lib/thor/task.rb:22:in `run'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/thor-0.14.6/lib/thor/invocation.rb:118:in `invoke_task'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/thor-0.14.6/lib/thor/invocation.rb:124:in `block in invoke_all'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/thor-0.14.6/lib/thor/invocation.rb:124:in `each'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/thor-0.14.6/lib/thor/invocation.rb:124:in `map'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/thor-0.14.6/lib/thor/invocation.rb:124:in `invoke_all'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/thor-0.14.6/lib/thor/group.rb:226:in `dispatch'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/thor-0.14.6/lib/thor/invocation.rb:109:in `invoke'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/thor-0.14.6/lib/thor/group.rb:269:in `block in _invoke_for_class_method'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/thor-0.14.6/lib/thor/shell.rb:74:in `with_padding'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/thor-0.14.6/lib/thor/group.rb:258:in `_invoke_for_class_method'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/thor-0.14.6/lib/thor/group.rb:150:in `_invoke_from_option_orm'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/thor-0.14.6/lib/thor/task.rb:22:in `run'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/thor-0.14.6/lib/thor/invocation.rb:118:in `invoke_task'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/thor-0.14.6/lib/thor/invocation.rb:124:in `block in invoke_all'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/thor-0.14.6/lib/thor/invocation.rb:124:in `each'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/thor-0.14.6/lib/thor/invocation.rb:124:in `map'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/thor-0.14.6/lib/thor/invocation.rb:124:in `invoke_all'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/thor-0.14.6/lib/thor/group.rb:226:in `dispatch'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/thor-0.14.6/lib/thor/base.rb:389:in `start'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/railties-3.0.12/lib/rails/generators.rb:163:in `invoke'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/railties-3.0.12/lib/rails/commands/generate.rb:10:in `<top (required)>'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/activesupport-3.0.12/lib/active_support/dependencies.rb:242:in `require'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/activesupport-3.0.12/lib/active_support/dependencies.rb:242:in `block in require'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/activesupport-3.0.12/lib/active_support/dependencies.rb:225:in `block in load_dependency'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/activesupport-3.0.12/lib/active_support/dependencies.rb:597:in `new_constants_in'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/activesupport-3.0.12/lib/active_support/dependencies.rb:225:in `load_dependency'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/activesupport-3.0.12/lib/active_support/dependencies.rb:242:in `require'
            from /home/sanchez/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/railties-3.0.12/lib/rails/commands.rb:17:in `<top (required)>'
            from script/rails:6:in `require'
            from script/rails:6:in `<main>'

这是我的 database.yml 文件

# MySQL.  Versions 4.1 and 5.0 are recommended.
#
# Install the MySQL driver:
#   gem install mysql2
#
# And be sure to use new-style password hashing:
#   http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: ProjectName_development
pool: 5
username: root
password: ***
socket: /var/run/mysqld/mysqld.sock

# 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:
adapter: mysql2
encoding: utf8
reconnect: false
database: ProjectName_test
pool: 5
username: root
password: ***
socket: /var/run/mysqld/mysqld.sock

production:
adapter: mysql2
encoding: utf8
reconnect: false
database: ProjectName_production
pool: 5
username: root
password: ***
socket: /var/run/mysqld/mysqld.sock

还有我的 GEMFILE

source 'http://rubygems.org'
gem 'rails', '3.0.12'
gem 'mysql2', '~> 0.2.17'

我在 linux mint 16 上使用 ruby​​ 1.9.2p320 和 Rails 3.0.12,也许这是一个愚蠢的错误,但我越来越绝望了。

【问题讨论】:

  • YAML 是否正确缩进?看起来不像。
  • development: 下面的行直到下一个空行应该缩进。
  • 是的!就是这样,谢谢=D

标签: mysql ruby-on-rails ruby


【解决方案1】:
# Gemfile
gem 'mysql2'

# config/database.yml
development:
  adapter: mysql2
  encoding: utf8
  database: projectName_development
  pool: 5
  username: root
  password: "123"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-13
    • 2020-02-09
    • 2012-01-12
    • 2013-04-18
    • 2011-05-08
    相关资源
    最近更新 更多