1.创建首页
1.新增一个控制器
`rails generate controller home index`
执行之后,在项目的app\views\home目录下就多了一个index.html.erb的文件
2.设置主页
1.编辑config\routes.rb添加以下代码
root
:to =>
"home#index"
2.在终端中
$ bundle install
$ rails s
此时发现主页变了
2.配置数据库
编辑config\database.yml,修改default和development,其它部分不要动
default:
&default
adapter:
mysql2
encoding:
utf8
pool:
<%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username:
root
password:
159753
socket:
/var/run/mysqld/mysqld.sock
development:
<<: *default
database: helloworld_development
3.生成器的使用
1.使用scaffold(汉译:脚手架,断头台)代码生成器创建user类
$ rails generate scaffold user
name:
text password:
text
因为前面测试的时候已经创建过user了,因此加上了一个force参数,表示修改这个类
2.创建项目所需数据库
$ rake
db:create
3.持久化类
$ rake
db:migrate
这个时候打开mysql
mysql
-u root
-p
use database helloworld_development
desc users
执行这一步后会自动在数据库中生成users表,注意了,我们创建的类是user,而这里自动生成的表却是users,是它的负数形式,是不是特别人性化?要么说ruby是最优雅的语言呢///果然名不虚传呀。。。
我们会发现users表的结构是这样的
| id | bigint(20) | NO | PRI | NULL | auto
_increment |
| name | text | YES | | NULL | |
| password | text | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
5 rows in set (0.00 sec)
我们发现除了我们自己添加的属性name,password之外,又多了三个属性:id(primaryKey),created_at,updated_at
4.将users/index设置为主页
编辑config--->routes.rb
将users/index设置为主页的代码为添加
root
:to =>
"users#index"
5.运行项目
$ bundle install
$ rails s
夸我夸我!!!
第一个hellworld项目总算是搭完了,感觉自己这几天神挡杀神佛挡杀佛。。。夸我夸我
这是我拿来练手的项目,github地址为 https://github.com/houchuanhao/helloworld.git
这个项目会作成一个论坛,欢迎fork~
Rails Migration Data Types – MySql – Postgresql – SQLite
Rails mysql postgresql sqlite
:
binary blob bytea blob
:
boolean tinyint(
1)
boolean boolean
:
date date date date
:datetime datetime timestamp datetime
:
decimal decimal decimal decimal
:float float float float
:
integer int(
11)
integer integer
:
string varchar(
255) * varchar(
255)
:
text text text text
:time time time datetime
:timestamp datetime timestamp datetime