Ruby on Rails学习笔记(二 )

来源:互联网 发布:java 注释 link 编辑:程序博客网 时间:2024/05/21 22:37

access the database(under the example of mysql)


there is a course about installing mysql on ubuntu
a course about installing mysql on ubuntu

Ruby provides Database Driven(dbd) for each database,
we have got the bastic dbd ,in order to get

install dbi(database interface)

command
gem install dbi

instal dbd of mysql(database driven)

gem install dbd-mysql
install to package
libmysqlclient-dev
ruby-dev

input the command in Terminal after installing

sudo apt-get install libmysqlclient-dev ruby-dev

you must add “sudo” on the commond

you won’t get enough root without “sudo” before the commond

在数据库中创建表

create table users(
id int(10) unsigned not null auto_increment,
username text default null,
password text default null,
primary key (id)
) engine=InnoDB default charset=utf8;

rails generate scaffold username:text password:text id:int

rails generate scaffold user name

1 以上语句是通过脚手架创建资源

创建user表,表里面有两个属性,分别是name,password,其中password类型为text
generate 生成,形成,造成;产生物理反应;产生(后代);引起
scaffold 支架,脚手架;<史>断头台

2 通过rake命令将创建的资源添加到数据库中

  1. 创建数据库
    rake db:create
  2. 更新数据库中的表结构
    rake db:migrate

    3修改主页

    编辑config目录下的routes.rb文件,添加代码

root 'user#index'
0 0
原创粉丝点击