微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

rails2.02快速体验

来自:JavaEye热点话题
作者:JavaEye网站发表于:2008-01-09 14:09:37.0
pesome最先收藏于2008-01-13 19:53:22.0


网站: JavaEye 作者: ozzzzzz 链接http://www.javaeye.com/topic/154371 发表时间: 2008年01月09日
声明:本文系JavaEye网站发布的原创文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!

如果是windows下的用户,而又不是使用instant Rail,那么需要进行以下步骤。

1、下载sqlite的exe和dll文件,然后将其放入系统path。(有些linux发行版本认安装了sqlite,无需再次安装)

2、确定你下载的sqlite版本,如果是sqlite3(注意放入path目录的文件应该保持的sqlite3.exe和sqlite3.dll,不要改名为sqlite.exe和sqlite.dll),在命令行运行

gem install sqlite3-ruby

安装sqlite3的ruby驱动。

3、新建一个Rails程序

本想自己写点代码,可是网上有个5行的todo,我就懒了。

rails todo

这时使用的是认的sqlite3数据库。如果你希望使用MysqL,则输入

rails todo -d MysqL

有点rails经验的人会发现这个“-d”的新东西。如果你是在MysqL下,往往需要修改config目录下的database.yml文件

development:
  adapter: MysqL
  encoding: utf8
  database: blog_development
  username: root
  password: root
  socket: /opt/local/var/run/MysqL5/MysqLd.sock

test:
  adapter: MysqL
  encoding: utf8
  database: blog_test
  username: root
  password: root
  socket: /opt/local/var/run/MysqL5/MysqLd.sock

production:
  adapter: MysqL
  encoding: utf8
  database: blog_production
  username: root
  password: root
  socket: /opt/local/var/run/MysqL5/MysqLd.sock
不过有些人觉得这样很不爽,于是有了这样的
defaults: &defaults
  adapter: MysqL
  encoding: utf8
  username: root
  password: root
  socket: /opt/local/var/run/MysqL5/MysqLd.sock

development:
  database: blog_development
  <<: *defaults

test:
  database: blog_test 
  <<: *defaults

production:
  database: blog_production
  <<: *defaults

当然出于安全考虑,谁也不会用这样的配置去搞到生产环境下。不过这样看着确实爽多了。

2、新建数据库

既然上面配置好了,那么下面就该实际的联起来用了。

cd todo
rake db:create:all

这里又一个新东西“rake db:create:all”,它将给你建立起各个数据库,现在不需要你自己去手工搞了。是不是比以前爽了。

D:/work/todo>rake db:create:all
(in D:/work/todo)
"db/development.sqlite3 already exists"
"db/production.sqlite3 already exists"
"db/test.sqlite3 already exists"
上面是我这里运行成功的提示

下面是个说明

db:charset	Retrieves the charset for the current environment’s database 
db:collation	 Retrieves the collation for the current environment’s database  
db:create	 Create the database defined in config/database.yml for the current RAILS_ENV
db:create:all	Create all the local databases defined in config/database.yml
db:drop		  Drops the database for the current RAILS_ENV
db:drop:all	 Drops all the local databases defined in config/database.yml
db:reset	  Drops and recreates the database from db/schema.rb for the current environment.
db:rollback	 Rolls the schema back to the prevIoUs version. Specify the number of steps with STEP=n
db:version	 Retrieves the current schema version number

这里注意有了个新的“db:rollback”命令,比以前用爽多了。

rake db:migrate VERSION=xxx
可以说byebye了。

3、真正的算代码的东西就一行

ruby script/generate scaffold Todo title:string body:text done:boolean due:datetime

前几个月大家还在感叹model里面竟然可以那样sexyness,现在看看这个直接在命令行搞定,现在该用啥词形容好呢。

最后别忘记

rake db:migrate

4、运行起来看看。

ruby script/server

然后用浏览器访问下面的链接127.0.0.1:3000/todos

搞定了一个todolist。

原文地址:https://www.jb51.cc/sqlite/203213.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐