参考
简介
Ruby on Rails,也简称 RoR 或 Rails,是一个使用Ruby语言写的开源网络应用 框架,它是严格按照MVC结构开发的。它努力使自身保持简单,来使实际的应用开 发时的代码更少,使用最少的配置。
Rails的设计原则包括 “不要重复自己” (Don't Repeat Yourself)和 “约定胜 于配置” (Convention Over Configuration)
安装
安装很简单,Fedora上可以 :
$ sudo yum install rubygem-rails
如果系统上有 gem 命令 ( Ruby 的一个 工具) ,也可以这样安装 :
$ gem install rails
第一个 Rails 应用
如果当前系统上已经安装好 ruby 和 rails,只要简单的3条命令即可创建一个 RoR web 应用。
$ rails myapp $ cd myapp $ ruby script/server
现在访问 http://127.0.0.1:3000 就可以看到默认的 RoR 界面了。如果没有看 到 "Welcome aboard" 界面,请首先检查 ruby 和 rails 是否安装正确,再检查 上面3条命令执行是否有错误。务必确认这些在看下面学习。
简单流程
创建项目
$ mkdir -v rails mkdir: 已创建目录 "rails" $ cd rails/ $ rails blog ... $ ls blog
“rails blog” 命令会创建 “blog” 项目,默认使用 SQLite 数据库,如果想要使 用 MySQL 数据库,使用这个命令创建项目:
$ rails blog -d mysql
同样,如果要使用 PostgreSQL 数据库,这样创建:
$ rails blog -d postgresql
使用 SQLite 数据库最简单,这个测试例子中,我们使用它。
现在当前目录下应该有一个 “blog” 目录,下面的操作都将基于这个目录,进入 这个目录。
$ cd blog
项目目录结构
默认 “blog” 下面有一些目录,任何 Rails 项目都将有类似的目录架构。
| 文件/目录 | 描述 |
|---|---|
| README | 简短描述这个项目的情况 |
| Rakefile | 同 Makefile 作用一样,能自动处理批任务 |
| app/ | 包含 controllers 、 models 、 views ,主要的工作在这里 |
| config/ | 配置本项目的 rules 、 routes 、 database 和其他 |
| db/ | 数据库相关 |
| doc/ | 文档相关 |
| lib/ | 本项目的一些扩展库 |
| log/ | 存放项目运行中的log |
| public/ | 存放一些静态文件,css,images,js等 |
| test/ | 测试模块 |
| tmp/ | 临时文件目录 |
| vendor/ | 存放第三方依赖包,请查看下面文件就知道含义了 |
配置数据库
基本每个 Rails 项目(应用程序)都会使用数据库。数据库的配置在 config/database.yml , 通常这个文件中包含三个配置类:
- development
- 我们开发的时候使用这个配置指定的数据库设置
- test
- 测试的时候使用的数据库设置
- production
- 发布后,即生产中使用的数据库设置
使用 SQLite 数据库
SQLite 是最简单的数据库,没有认证,所有的资料存在一个文件中。在开发和测 试中,我们可以使用 SQLite 数据库。
development: adapter: sqlite3 database: db/development.sqlite3 pool: 5 timeout: 5000
使用 MySQL 数据库
下面是我安装 open SUSE Building System 时,一段使用 MySQL 数据库的实例:
9 production_slave: 10 adapter: mysql 11 database: frontend_production 12 username: root 13 password: turbolinux 14 socket: /var/lib/mysql/mysql.sock
要把 username/password 和 database 换成你自己的。
使用 PostgreSQL 数据库
PostgreSQL 我最喜欢,只要有可能我就会使用它。配置类似:
development: adapter: postgresql encoding: unicode database: blog_development pool: 5 username: blog password:
初始化数据库
现在在项目主目录 "blog" 下,用 “rake db:create” 初始化数据库:
$ rake db:create (in /home/jianlee/lab/rails/blog) rake aborted! no such file to load -- sqlite3 (See full trace by running task with --trace)
出现上面错误是我还没有安装 sqlite 的 ruby 支持,Fedora下安装:
sudo yum install ruby-sqlite3
也可以用 gem 命令安装 :
$ gem install sqlite3-ruby
现在再次执行 "rake db:create" 命令:
$ rake db:create (in /home/jianlee/lab/rails/blog)
在你的项目下使用 “rake -T” 可以看到你能使用的命令列表:
$ rake -T (in /home/jianlee/lab/rails/blog) rake db:abort_if_pending_migrations # Raises an error if there are pending migrations rake db:charset # Retrieves the charset for the current environment's database rake db:collation # Retrieves the collation for the current environment's database rake db:create # Create the database defined in config/database.yml for the current RAILS... rake db:create:all # Create all the local databases defined in config/database.yml rake db:drop # Drops the database for the current RAILS_ENV rake db:drop:all # Drops all the local databases defined in config/database.yml ...
“你好,Rails!”
任何的经典的程序语言示例都是从输出一条信息开始,这里我们就用 “Hello, World!” 的中文版 :-) 。
我们先用工具常见一个最小化的 controller 和 view :
jianlee@localhost:~/lab/rails/blog$ script/generate controller home index
exists app/controllers/
exists app/helpers/
create app/views/home
exists test/functional/
create test/unit/helpers/
create app/controllers/home_controller.rb
create test/functional/home_controller_test.rb
create app/helpers/home_helper.rb
create test/unit/helpers/home_helper_test.rb
create app/views/home/index.html.erb
我们看到上面命令创建了一些目录和文件,其他都不用关心,现在打开 app/views/home/index.html.erb 文件,仅仅输入:“你好!Rails!”。
jianlee@localhost:~/lab/rails/blog$ vim app/views/home/index.html.erb
启动 Web 服务器
Rails 内置一个简单的 Web Server,启动它即可:
jianlee@localhost:~/lab/rails/blog$ script/server => Booting WEBrick => Rails 2.3.3 application starting on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown server [2009-09-01 15:15:51] INFO WEBrick 1.3.1 [2009-09-01 15:15:51] INFO ruby 1.8.6 (2009-06-08) [i386-linux] [2009-09-01 15:15:51] INFO WEBrick::HTTPServer#start: pid=5034 port=3000 ...
现在访问 : http://127.0.0.1:3000 将看到 Rails 的默认主页。
访问我们刚才写的网页 : http://127.0.0.1:3000/home/index ,就能看到 “你 好!Rails!” 信息了!
设置项目的主页
现在你不想再用Rails的默认主页,想使用自己写的主页了。
第一步,删除 public/index.html 文件
jianlee@localhost:~/lab/rails/blog$ rm public/index.html
第二步,编辑 config/routes.rb 文件,这个文件默认在结尾有:
map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format' end
要使用我们刚才看见的 home 主页,添加下面一行到 config/routes.rb 文件里 “end” 之前:
map.root :controller => "home"
现在访问 : http://127.0.0.1:3000 就可以看到你自己的主页了。如果还是显 示 Welcome Aboard, 请刷新浏览器试试!
更进一步,使用 Scaffolding
根据官方的资料,很多高手不会用这里的快速开发工具的,但是它们真的很快, 下面试验一把:
现在可以更进一步用 Rails 的一些实用工具给我们的 “blog” 添加一些功能。
$ script/generate scaffold Post name:string title:string content:text
然后运行 “rake db:migrate” 初始化数据库:
$ rake db:migrate
修改 app/views/home/index.html.erb 文件为下面内容:
<h1>你好!Rails!</h1> <%= link_to "My Blog", posts_path %>
现在启动 Web 服务器,然后访问 http://127.0.0.1:3000
$ script/server
快快发送你的第一个帖子试试!简单吧!
