只适用于window平台:
假设之前你已经装好了ruby,rails,mysql
1) 启动mysql
d:\>net start mysql
2) 优化mysql
d:\>gem install mysql
这会提示出很多版本,确保你选择的是高版本而且是以(mswin32)结尾的产品
3) 创建rails项目
d:\>rails cookbook
进入cookbook目录
这样子默认使用的是mysql数据库,如果想使用PostgreSQL,
就要这样子指定它创建项目了d:>rails cookbook --database=postgresql
d:\>cd cookbook
4) 数据库test创建表languages,我的数据库的用户名是root,密码是root
我们使用rails创建表
1:打开cookbook\config\database.yml修改为
development:
adapter: mysql
database: test
username: root
password: root
host: localhost
# 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: mysql
database: cookbook_test
username: root
password: root
host: localhost
production:
adapter: mysql
database: cookbook_production
username: root
password: root
host: localhost
2:创建一个migration脚本
d:\cookbokk>ruby script/generate migration build_db
打开db/migrate/001_build_db.rb
修改如下:
class BuildDb < ActiveRecord::Migration
def self.up
create_table :languages,:force=>true do |t|
t.column :name,:string
t.column :description,:string
end
end
def self.down
drop_table :languages
end
end
3:执行d:\cookbook>
rake db:migrate
这时你的数据库表应该创建了
5)有两种方式创建scaffolding
<第一种方式>
1)ruby script/generate model language
2)ruby script/generate controller language
3)修改这个文件为如下内容app/controllers/language_controller.rb:
class LanguageController < ApplicationController
scaffold :languages
end
4)启动服务器ruby script/server
5)访问http://localhost:3000/languages
<第二种方式>
1)ruby script/generate scaffold language
2) 启动服务器ruby script/server
3)访问http://localhost:3000/languages