分享

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

 耍库 2008-08-22

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

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

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

Ruby代码 复制代码
  1. gem install sqlite3-ruby  

 安装sqlite3的ruby驱动。

3、新建一个Rails程序

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

Ruby代码 复制代码
  1. rails todo  

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

Ruby代码 复制代码
  1. rails todo -d mysql  

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

Ruby代码 复制代码
  1. development:   
  2.   adapter: mysql   
  3.   encoding: utf8   
  4.   database: blog_development   
  5.   username: root   
  6.   password: root   
  7.   socket: /opt/local/var/run/mysql5/mysqld.sock   
  8.   
  9. test:   
  10.   adapter: mysql   
  11.   encoding: utf8   
  12.   database: blog_test   
  13.   username: root   
  14.   password: root   
  15.   socket: /opt/local/var/run/mysql5/mysqld.sock   
  16.   
  17. production:   
  18.   adapter: mysql   
  19.   encoding: utf8   
  20.   database: blog_production   
  21.   username: root   
  22.   password: root   
  23.   socket: /opt/local/var/run/mysql5/mysqld.sock  
 不过有些人觉得这样很不爽,于是有了这样的
Ruby代码 复制代码
  1. defaults: &defaults   
  2.   adapter: mysql   
  3.   encoding: utf8   
  4.   username: root   
  5.   password: root   
  6.   socket: /opt/local/var/run/mysql5/mysqld.sock   
  7.   
  8. development:   
  9.   database: blog_development   
  10.   <<: *defaults   
  11.   
  12. test:   
  13.   database: blog_test    
  14.   <<: *defaults   
  15.   
  16. production:   
  17.   database: blog_production   
  18.   <<: *defaults  

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

2、新建数据库

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

Ruby代码 复制代码
  1. cd todo   
  2. rake db:create:all  

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

Ruby代码 复制代码
  1. D:\work\todo>rake db:create:all  
  2. (in D:/work/todo)   
  3. "db/development.sqlite3 already exists"  
  4. "db/production.sqlite3 already exists"  
  5. "db/test.sqlite3 already exists"  
 上面是我这里运行成功的提示。

下面是个说明

Ruby代码 复制代码
  1. db:charset  Retrieves the charset for the current environment’s database    
  2. db:collation     Retrieves the collation for the current environment’s database     
  3. db:create    Create the database defined in config/database.yml for the current RAILS_ENV   
  4. db:create:all   Create all the local databases defined in config/database.yml   
  5. db:drop       Drops the database for the current RAILS_ENV   
  6. db:drop:all  Drops all the local databases defined in config/database.yml   
  7. db:reset      Drops and recreates the database from db/schema.rb for the current environment.   
  8. db:rollback  Rolls the schema back to the previous version. Specify the number of steps with STEP=n   
  9. db:version   Retrieves the current schema version number  

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

Ruby代码 复制代码
  1. rake db:migrate VERSION=xxx  
 可以说byebye了。

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

Ruby代码 复制代码
  1. ruby script/generate scaffold Todo title:string body:text done:boolean due:datetime  

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

最后别忘记

Ruby代码 复制代码
  1. rake db:migrate  

4、运行起来看看。

Ruby代码 复制代码
  1. ruby script/server  

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

搞定了一个todolist。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多