@@ -17,16 +17,16 @@ Rails 入门
17
17
18
18
本文针对想从零开始开发 Rails 程序的初学者,不需要预先具备任何的 Rails 使用经验。不过,为了能顺利阅读,还是需要事先安装好一些软件:
19
19
20
- * [ Ruby] ( http ://www.ruby-lang.org/en/downloads) 1.9.3 及以上版本
21
- * 包管理工具 [ RubyGems] ( http ://rubygems.org) ,随 Ruby 1.9+ 安装。想深入了解 RubyGems,请阅读 [ RubyGems 指南] ( http://guides.rubygems.org )
22
- * [ SQLite3] ( http ://www.sqlite.org) 数据库
20
+ * [ Ruby] ( https ://www.ruby-lang.org/en/downloads) 1.9.3 及以上版本
21
+ * 包管理工具 [ RubyGems] ( https ://rubygems.org) ,随 Ruby 1.9+ 安装。想深入了解 RubyGems,请阅读 [ RubyGems 指南] ( http://guides.rubygems.org )
22
+ * [ SQLite3] ( https ://www.sqlite.org) 数据库
23
23
24
24
Rails 是使用 Ruby 语言开发的网页程序框架。如果之前没接触过 Ruby,学习 Rails 可要深下一番功夫。网上有很多资源可以学习 Ruby:
25
25
26
26
* [ Ruby 语言官方网站] ( https://www.ruby-lang.org/zh_cn/documentation/ )
27
27
* [ reSRC 列出的免费编程书籍] ( http://resrc.io/list/10/list-of-free-programming-books/#ruby )
28
28
29
- 记住,某些资源虽然很好,但肯针对 Ruby 1.8,甚至 1.6,所以没有介绍一些 Rails 日常开发会用到的句法。
29
+ 记住,某些资源虽然很好,但是针对 Ruby 1.8,甚至 1.6 编写的 ,所以没有介绍一些 Rails 日常开发会用到的句法。
30
30
31
31
Rails 是什么?
32
32
-------------
@@ -53,16 +53,16 @@ TIP: 文中的示例代码使用 `$` 表示命令行提示符,你的提示符
53
53
54
54
打开命令行:在 Mac OS X 中打开 Terminal.app,在 Windows 中选择“运行”,然后输入“cmd.exe”。下文中所有以 ` $ ` 开头的代码,都要在命令行中运行。先确认是否安装了 Ruby 最新版:
55
55
56
- TIP: 有很多工具可以帮助你快速在系统中安装 Ruby 和 Ruby on Rails。Windows 用户可以使用 [ Rails Installer] ( http://railsinstaller.org ) ,Mac OS X 用户可以使用 [ Rails One Click ] ( http ://railsoneclick .com) 。
56
+ TIP: 有很多工具可以帮助你快速在系统中安装 Ruby 和 Ruby on Rails。Windows 用户可以使用 [ Rails Installer] ( http://railsinstaller.org ) ,Mac OS X 用户可以使用 [ Tokaido ] ( https ://github .com/tokaido/tokaidoapp ) 。
57
57
58
58
``` bash
59
59
$ ruby -v
60
- ruby 2.0.0p353
60
+ ruby 2.1.2p95
61
61
```
62
62
63
63
如果你还没安装 Ruby,请访问 [ ruby-lang.org] ( https://www.ruby-lang.org/en/downloads/ ) ,找到针对所用系统的安装方法。
64
64
65
- 很多类 Unix 系统都自带了版本尚新的 SQLite3。Windows 等其他操作系统的用户可以在 [ SQLite3 的网站] ( http ://www.sqlite.org) 上找到安装说明。然后,确认是否在 PATH 中:
65
+ 很多类 Unix 系统都自带了版本尚新的 SQLite3。Windows 等其他操作系统的用户可以在 [ SQLite3 的网站] ( https ://www.sqlite.org) 上找到安装说明。然后,确认是否在 PATH 中:
66
66
67
67
``` bash
68
68
$ sqlite3 --version
@@ -113,7 +113,7 @@ $ cd blog
113
113
| config/| 设置程序的路由,数据库等。详情参阅“[ 设置 Rails 程序] ( /configuring.html ) ”一文。|
114
114
| config.ru| 基于 Rack 服务器的程序设置,用来启动程序。|
115
115
| db/| 存放当前数据库的模式,以及数据库迁移文件。|
116
- | Gemfile, Gemfile.lock| 这两个文件用来指定程序所需的 gem 依赖件,用于 Bundler gem。关于 Bundler 的详细介绍,请访问 [ Bundler 官网] ( http://gembundler.com ) 。|
116
+ | Gemfile, Gemfile.lock| 这两个文件用来指定程序所需的 gem 依赖件,用于 Bundler gem。关于 Bundler 的详细介绍,请访问 [ Bundler 官网] ( http://bundler.io ) 。|
117
117
| lib/| 程序的扩展模块。|
118
118
| log/| 程序的日志文件。|
119
119
| public/| 唯一对外开放的文件夹,存放静态文件和编译后的资源文件。|
@@ -172,8 +172,6 @@ invoke test_unit
172
172
create test/controllers/welcome_controller_test.rb
173
173
invoke helper
174
174
create app/helpers/welcome_helper.rb
175
- invoke test_unit
176
- create test/helpers/welcome_helper_test.rb
177
175
invoke assets
178
176
invoke coffee
179
177
create app/assets/javascripts/welcome.js.coffee
@@ -234,7 +232,7 @@ TIP: 关于路由的详细介绍,请阅读“[Rails 路由全解](/routing.htm
234
232
Rails 提供了一个 ` resources ` 方法,可以声明一个符合 REST 架构的资源。创建文章资源后,` config/routes.rb ` 文件的内容如下:
235
233
236
234
``` ruby
237
- Blog :: Application .routes.draw do
235
+ Rails .application .routes.draw do
238
236
239
237
resources :articles
240
238
245
243
执行 ` rake routes ` 任务,会看到定义了所有标准的 REST 动作。输出结果中各列的意义稍后会说明,现在只要留意 ` article ` 的单复数形式,这在 Rails 中有特殊的含义。
246
244
247
245
``` bash
248
- $ rake routes
246
+ $ bin/ rake routes
249
247
Prefix Verb URI Pattern Controller#Action
250
248
articles GET /articles(.:format) articles#index
251
249
POST /articles(.:format) articles#create
@@ -273,7 +271,7 @@ edit_article GET /articles/:id/edit(.:format) articles#edit
273
271
产生这个错误的原因是,没有定义用来处理该请求的控制器。解决这个问题的方法很简单:创建名为 ` ArticlesController ` 的控制器。执行下面的命令即可:
274
272
275
273
``` bash
276
- $ rails g controller articles
274
+ $ bin/ rails g controller articles
277
275
```
278
276
279
277
打开刚生成的 ` app/controllers/articles_controller.rb ` 文件,会看到一个几乎没什么内容的控制器:
@@ -296,7 +294,9 @@ NOTE: 在 Ruby 中,方法分为 `public`、`private` 和 `protected` 三种,
296
294
手动创建动作只需在控制器中定义一个新方法。打开 ` app/controllers/articles_controller.rb ` 文件,在 ` ArticlesController ` 类中,定义 ` new ` 方法,如下所示:
297
295
298
296
``` ruby
299
- def new
297
+ class ArticlesController < ApplicationController
298
+ def new
299
+ end
300
300
end
301
301
```
302
302
@@ -369,7 +369,7 @@ Missing template articles/new, application/new with {locale:[:en], formats:[:htm
369
369
这里,我们把 ` :url ` 选项的值设为 ` articles_path ` 帮助方法。要想知道这个方法有什么作用,我们要回过头再看一下 ` rake routes ` 的输出:
370
370
371
371
``` bash
372
- $ rake routes
372
+ $ bin/ rake routes
373
373
Prefix Verb URI Pattern Controller#Action
374
374
articles GET /articles(.:format) articles#index
375
375
POST /articles(.:format) articles#create
429
429
在 Rails 中,模型的名字使用单数,对应的数据表名使用复数。Rails 提供了一个生成器用来创建模型,大多数 Rails 开发者创建模型时都会使用。创建模型,请在终端里执行下面的命令:
430
430
431
431
``` bash
432
- $ rails generate model Article title:string text:text
432
+ $ bin/ rails generate model Article title:string text:text
433
433
```
434
434
435
435
这个命令告知 Rails,我们要创建 ` Article ` 模型,以及一个字符串属性 ` title ` 和文本属性 ` text ` 。这两个属性会自动添加到 ` articles ` 数据表中,映射到 ` Article ` 模型。
@@ -464,7 +464,7 @@ TIP: 关于迁移的详细说明,请参阅“[Active Record 数据库迁移](/
464
464
然后,使用 rake 命令运行迁移:
465
465
466
466
``` bash
467
- $ rake db:migrate
467
+ $ bin/ rake db:migrate
468
468
```
469
469
470
470
Rails 会执行迁移操作,告诉你创建了 ` articles ` 表。
0 commit comments