我是个新手,刚开始看的第一份文档是Eiffel Qiu的《rubyonrails》一文,其中实现了一个简单的mybook例子,让人快速的体验ROR的魔法般的魅力,但是由于我下载安装的是最新发布的rails2.0,据说很多方法和插件在2.0下都不能继续使用了,比如scaffold,render_text等等,所以导致我一直没有成功的实现这个例子,正在郁闷之中,我看到了ozzzzzz在论坛里面写了一篇《rails2.02快速体验》,试验之下果然可以,写出来与各位刚入手的兄弟共享,高手大大可以跳过了。。。
==============================================================
1、如果已经安装了sqlite3的话可以跳到第3步,否则下载sqlite3.exe和sqlite3.dll(已上传这两个文件,在页面最下面有下载)两个文件放入ruby的安装目录下的bin目录,比如我的是D:\ruby\bin\
2、执行以下命令
gem install sqlite3-ruby
安装sqlite3的ruby驱动。
3、现在就可以进行ROR开发了。比如我这里要实现mybook,首先执行
rails mybook -d mysql
之所以加上"-d mysql"是将数据库改为mysql(默认为sqlite3),然后要修改/mybook/config/下面的database.yml,比如我的修改如下:
- development:
- adapter: mysql
- encoding: utf8
- database: mybook_development
- username: root
- password: root
- host: localhost
-
- test:
- adapter: mysql
- encoding: utf8
- database: mybook_test
- username: root
- password: root
- host: localhost
-
- production:
- adapter: mysql
- encoding: utf8
- database: mybook_production
- username: root
- password: root
- host: localhost
development:
adapter: mysql
encoding: utf8
database: mybook_development
username: root
password: root
host: localhost
test:
adapter: mysql
encoding: utf8
database: mybook_test
username: root
password: root
host: localhost
production:
adapter: mysql
encoding: utf8
database: mybook_production
username: root
password: root
host: localhost
4、建立数据库,执行
- cd mybook
- rake db:create:all
cd mybook
rake db:create:all
,成功的话cmd会显示:
- D:\rubyapps\mybook>rake db:create:all
- (in D:/rubyapps/mybook)
D:\rubyapps\mybook>rake db:create:all
(in D:/rubyapps/mybook)
此时查看MySQL可以看到多出来三个表,如下图所示:
5、建立model
- D:\rubyapps\mybook>ruby script/generate scaffold Mybook title:string description:text buydate:datetime
D:\rubyapps\mybook>ruby script/generate scaffold Mybook title:string description:text buydate:datetime
最后
rake db:migrate
,成功的话会有:
- (in D:/rubyapps/mybook)
- == 1 CreateMybooks: migrating =================================================
- -- create_table(:mybooks)
- -> 0.0780s
- == 1 CreateMybooks: migrated (0.0780s) ========================================
(in D:/rubyapps/mybook)
== 1 CreateMybooks: migrating =================================================
-- create_table(:mybooks)
-> 0.0780s
== 1 CreateMybooks: migrated (0.0780s) ========================================
此时查看MySQL,可以看到数据库结构如下:
表的结构如下:
6、运行一下看看吧:)
ruby script/server
然后在浏览器里面输入,我这里的效果如下:
阅读(747) | 评论(0) | 转发(0) |