首先新建rails项目,使用mysql数据库:
rails new Manage_Person -d mysql
项目建成功后,就生成脚手架,
rails generate scaffold Person id:int name:varchar\(45\) age:int sex:varchar\(10\)
\(..\)是转义,因为不能直接使用括号。
这时会报错如下:
invoke active_record
/home/djx/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.0.0/lib/active_record/connection_adapters/connection_specification.rb:58:in `rescue in resolve_hash_connection': Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile. (Gem::LoadError)
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.0.0/lib/active_record/connection_adapters/connection_specification.rb:55:in `resolve_hash_connection'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.0.0/lib/active_record/connection_adapters/connection_specification.rb:46:in `resolve_string_connection'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.0.0/lib/active_record/connection_adapters/connection_specification.rb:30:in `spec'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.0.0/lib/active_record/connection_handling.rb:39:in `establish_connection'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.0.0/lib/active_record/railtie.rb:175:in `block (2 levels) in <class:Railtie>'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/activesupport-4.0.0/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/activesupport-4.0.0/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/activesupport-4.0.0/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/activesupport-4.0.0/lib/active_support/lazy_load_hooks.rb:44:in `each'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/activesupport-4.0.0/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.0.0/lib/active_record/base.rb:322:in `<module:ActiveRecord>'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.0.0/lib/active_record/base.rb:22:in `<top (required)>'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/railties-4.0.0/lib/rails/generators/named_base.rb:172:in `pluralize_table_names?'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/railties-4.0.0/lib/rails/generators/named_base.rb:112:in `table_name'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.0.0/lib/rails/generators/active_record/model/model_generator.rb:18:in `create_migration_file'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/thor-0.19.1/lib/thor/command.rb:27:in `run'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `block in invoke_all'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `each'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `map'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `invoke_all'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/thor-0.19.1/lib/thor/group.rb:232:in `dispatch'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/thor-0.19.1/lib/thor/invocation.rb:115:in `invoke'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/thor-0.19.1/lib/thor/group.rb:277:in `block in _invoke_for_class_method'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/thor-0.19.1/lib/thor/shell.rb:68:in `with_padding'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/thor-0.19.1/lib/thor/group.rb:266:in `_invoke_for_class_method'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/thor-0.19.1/lib/thor/group.rb:133:in `_invoke_from_option_orm'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/thor-0.19.1/lib/thor/command.rb:27:in `run'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `block in invoke_all'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `each'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `map'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `invoke_all'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/thor-0.19.1/lib/thor/group.rb:232:in `dispatch'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/thor-0.19.1/lib/thor/base.rb:440:in `start'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/railties-4.0.0/lib/rails/generators.rb:156:in `invoke'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/railties-4.0.0/lib/rails/commands/generate.rb:11:in `<top (required)>'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `block in require'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'
from /home/djx/.rvm/gems/ruby-2.0.0-p598/gems/railties-4.0.0/lib/rails/commands.rb:49:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
这时只需打开Gemfile,在里面添加
gem 'mysql2','~> 0.3.18'
gem "iconv", "~> 1.0.3"
然后执行
bundle install
然后可能报错
An error occurred while installing mysql2 (0.3.20), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.3.20'` succeeds before bundling.
只需执行
gem install mysql2 -v '0.3.20'