rails2.02快速体验 - rails - Ruby - JavaEye论坛

来源:百度文库 编辑:神马文学网 时间:2024/04/29 21:12:40
如果是windows下的用户,而又不是使用instant Rail,那么需要进行以下步骤。
1、下载sqlite的exe和dll文件,然后将其放入系统path。(有些linux发行版本默认安装了sqlite,无需再次安装)
2、确定你下载的sqlite版本,如果是sqlite3(注意放入path目录的文件应该保持的sqlite3.exe和sqlite3.dll,不要改名为sqlite.exe和sqlite.dll),在命令行运行
Ruby代码
gem install sqlite3-ruby
gem install sqlite3-ruby
安装sqlite3的ruby驱动。
3、新建一个Rails程序
本想自己写点代码,可是网上有个5行的todo,我就懒了。
Ruby代码
rails todo
rails todo
这时使用的是默认的sqlite3做数据库。如果你希望使用mysql,则输入
Ruby代码
rails todo -d mysql
rails todo -d mysql
有点rails经验的人会发现这个“-d”的新东西。如果你是在mysql下,往往需要修改config目录下的database.yml文件。
Ruby代码
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
development:adapter: mysqlencoding: utf8database: blog_developmentusername: rootpassword: rootsocket: /opt/local/var/run/mysql5/mysqld.socktest:adapter: mysqlencoding: utf8database: blog_testusername: rootpassword: rootsocket: /opt/local/var/run/mysql5/mysqld.sockproduction:adapter: mysqlencoding: utf8database: blog_productionusername: rootpassword: rootsocket: /opt/local/var/run/mysql5/mysqld.sock 不过有些人觉得这样很不爽,于是有了这样的Ruby代码
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
defaults: &defaultsadapter: mysqlencoding: utf8username: rootpassword: rootsocket: /opt/local/var/run/mysql5/mysqld.sockdevelopment:database: blog_development<<: *defaultstest:database: blog_test<<: *defaultsproduction:database: blog_production<<: *defaults
当然出于安全考虑,谁也不会用这样的配置去搞到生产环境下。不过这样看着确实爽多了。
2、新建数据库
既然上面配置好了,那么下面就该实际的联起来用了。
Ruby代码
cd todo
rake db:create:all
cd todorake db:create:all
这里又一个新东西“rake db:create:all”,它将给你建立起各个数据库,现在不需要你自己去手工搞了。是不是比以前爽了。
Ruby代码
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"
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" 上面是我这里运行成功的提示。
下面是个说明
Ruby代码
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:charset Retrieves the charset for the current environment’s databasedb:collation Retrieves the collation for the current environment’s databasedb:create Create the database defined in config/database.yml for the current RAILS_ENVdb:create:all Create all the local databases defined in config/database.ymldb:drop Drops the database for the current RAILS_ENVdb:drop:all Drops all the local databases defined in config/database.ymldb: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=ndb:version Retrieves the current schema version number
这里注意有了个新的“db:rollback”命令,比以前用爽多了。
Ruby代码
rake db:migrate VERSION=xxx
rake db:migrate VERSION=xxx 可以说byebye了。
3、真正的算代码的东西就一行
Ruby代码
ruby script/generate scaffold Todo title:string body:text done:boolean due:datetime
ruby script/generate scaffold Todo title:string body:text done:boolean due:datetime
前几个月大家还在感叹model里面竟然可以那样sexyness,现在看看这个直接在命令行搞定,现在该用啥词形容好呢。
最后别忘记
Ruby代码
rake db:migrate
rake db:migrate
4、运行起来看看。
Ruby代码
ruby script/server
ruby script/server
然后用浏览器访问下面的链接127.0.0.1:3000/todos
搞定了一个todolist。