2007-10-28

rails创建工程

关键字: rails 学习

Rails 创建工程的一般步骤:
创建一个工程: rails 工程名(例如:CookBook)
启动这个工程: 在命令窗口,移动到CookBook目录下
输入: ruby script/server
创建一个控制器类: ruby script/generate controller MyTest, 产生的文件如下:my_test_controller.rb.
创建数据库:
由于rails定义了三个运行时的环境,所以要至少创建三个数据库(development, test, and production),
mysql -u root
create database XXX_dev;
create database XXX_test;
create database XXX_prod;
grant all privileages on XXX_dev .* to 'XX_user' @'localhost' identified by 'password';
grant all privileages on XXX_test.* to 'XX_user' @'localhost' identified by 'password';
grant all privileages on XXX_prod.* to 'XX_user' @'localhost' identified by 'password';

mysql> grant all privileges on *.* to test@localhost identified by 'test' with grant option;

这句增加一个本地具有所有权限的test用户(超级用户),密码是test。ON子句中的*.*意味着"所有数据库、所有表"。with grant option表示它具有grant权限。

http://jed.dzhope.com/read.php/22.htm
create database.sql(省略)
mysql XXX_dev -u XX_user -p password < database.sql

mysqladmin -u root create XXXX_database_name
mysql XXX_dev -u XX_user -p <<< "show tables;"
利用Migration创造数据库:
ruby script/generate migration create_database
Migration --数据迁移:
模型生成器 (model generator) 创建的 migration 迁移:ruby script/generate model 模型名, 会创建模型与表的关联。
只创建migration 本身:script/generate migration add_price_column
使用rake 的db:migrate 任务来运行migration迁移。
通过创建rails类的ActiveRecord::Migration的子类来完成Migration的迁移,每个类中至少包含一个up和down 方法,
class XXXXX < ActiveRecord::migration
def self.up
add_column :orders(表名), :email(列名), :string(类型)
end
def self.down
remove_column :orders, :email
end
end
migration 迁移支持的类型是: :binary :boolean :date :datetime :float :integer :string :text :time ,和 :timestamp

当在一个 migration 迁移内定义一个列时,你可指定三个选项。每个选项由 key=>value 对给出。

1 :null => true or false

如果为 true ,则基础列被添加一个不能为 null 的约束 ( 如果数据库支持的话 )

2 :limit => size

设置字段尺寸的限制。这基本上出现在用 string 创建数据库的列时。

3 :default => value

Renaming Columns ( 重命名列 )

scaffold :

脚手架是一种预定义的框架,它可以生成一个简单的CRUD结构,两种方式来定义:

一种是在控制器的文件下加上 scaffold :控制器名称, 例如:

class RecipeController < ApplicationController

scaffold :recipe

end

一种是:

创建控制器:

ruby script\server controller 表名

创建模型:

ruby script\server model 表名



评论
发表评论

您还没有登录,请登录后发表评论