diff --git a/rails_guides/helpers.rb b/rails_guides/helpers.rb
index d9a609c..fb695c5 100755
--- a/rails_guides/helpers.rb
+++ b/rails_guides/helpers.rb
@@ -6,14 +6,14 @@ def guide(name, url, options = {}, &block)
result = content_tag(:dt, link)
result <<
if options[:work_in_progress]
- content_tag(:dd, "施工中!贡献者: #{options[:contributor] || '匿名'}".html_safe, :class => 'work-in-progress')
+ content_tag(:dd, "施工中!贡献者: #{generate_contributors_link(options[:contributor])}".html_safe, :class => 'work-in-progress')
elsif !options[:contributor]
content_tag(:dd, :class => 'work-in-progress' ) do
"这篇还没有人翻译,我要翻译本文".html_safe
end
else
content_tag(:dd, :class => 'contributor' ) do
- "贡献者:#{options[:contributor] || '匿名'}".html_safe
+ "贡献者:#{generate_contributors_link(options[:contributor])}".html_safe
end
end
result << content_tag(:dd, capture(&block))
@@ -49,5 +49,16 @@ def code(&block)
c = capture(&block)
content_tag(:code, c)
end
+
+ private
+ def generate_contributors_link(contributors_string = '')
+ result = []
+ contributors = contributors_string.split(/\,\s/)
+ contributors.each do |name|
+ result << content_tag('a', name, :href => "https://github.com/#{name}")
+ end
+
+ return result.join(', ')
+ end
end
end
diff --git a/source/CN/getting_started.textile b/source/CN/getting_started.textile
index 73f569c..44ee919 100755
--- a/source/CN/getting_started.textile
+++ b/source/CN/getting_started.textile
@@ -1,4 +1,3 @@
-<<<<<<< HEAD
h2. Rails 初上手指南
本指导手册涉及了使用和运行 Ruby on Rails,通过阅读本指导,你会了解到:
@@ -1472,10 +1471,6 @@ Rails 同样也带有内置的帮助你可以使用 rake 命令实用工具在
h3. Configuration Gotchas配置陷阱
-The easiest way to work with Rails is to store all external data as UTF-8. If
-you don't, Ruby libraries and Rails will often be able to convert your native
-data into UTF-8, but this doesn't always work reliably, so you're better off
-ensuring that all external data is UTF-8.
使用 Rails 最简单的工作方式是存储所有的外部数据为 UTF-8编码。如果不那样做,Ruby libraries 和 Rails 通才会转换你的自然数据成 UTF-8 编码,但是这样不是很可靠,因此你最好保证所有的外部数据是 UTF-8 编码。如果你在这里犯了错误,一般的症状就是在浏览器中出现钻石符号(可能是^)变成了问号。 另一个普遍症状是 “ü” 变成了 “ü”。
Rails 在国际化上下了很多的功夫,大部分的此类错误都能够自动发现并错误,然而如果你有一些不是用 UTF-8 存储的特殊的数据恐怕就会出现一些奇怪的问题了。
@@ -1493,1499 +1488,3 @@ Rails 在国际化上下了很多的功夫,大部分的此类错误都能够
is using Latin-1 internally, and your user enters a Russian, Hebrew, or Japanese
character, the data will be lost forever once it enters the database. If possible,
use UTF-8 as the internal storage of your database.
-=======
-h2. Rails 初上手指南
-
-本指导手册涉及了使用和运行 Ruby on Rails,通过阅读本指导,你会了解到:
-
-* (怎样)安装,创建一个新的 Rails 应用程序,并且将你的应用程序连接到数据库
-* Rails 的一般(页面)布局
-* MVC 的基本原则和 RESTful 设计(理念)
-* 怎样迅速地开始一个 Rails 应用
-
-endprologue.
-
-WARNING. 这个指导手册适用与 Rails 3.2,有些代码在 Rails 早期版本可能无法正常运行。
-
-h3. 手册假设
-
-本指导设计给那些想大概的了解 Rails 应用创建的初学者。这里假设你对 Rails 没有任何预先的了解。要得到(本手册的)知识,你需要预先安装:
-
-* "Ruby":http://www.ruby-lang.org/en/downloads 1.8.7 或者更高版本
-
-TIP: 注意 Ruby 1.8.7 p248 和 p249 有 marshaling bugs ,会与 Rails 3.0 发生冲突。Ruby 1.8.7 p248 和 p249在 发行版 1.8.7-2010.02 已经得到解决。Ruby 1.9 下 Ruby 1.9.1 不能使用,因为它在 Rails 3.0 上会出现 segfaults 错误,因此如果你希望使用 Rails 3 和 1.9.x ,更新版本到 1.9.2 就可以安全地使用 Rails。
-
-
-* "RubyGems":http://rubyforge.org/frs/?group_id=126 包管理系统
- ** 如果你想了解更多关于 RubyGems 的知识,请阅读 "RubyGems User Guide":http://docs.rubygems.org/read/book/1
-* 安装 "SQLite3 Database":http://www.sqlite.org
-
-Rails 是一个基于 Ruby 程序语言的 web 程序框架。如果你没有预先的学习 Ruby,你会发现直接的入门 Rails 学习很有跨度。这里有一些免费 Ruby 的学习资源。
-
-* "Mr. Neighborly's Humble Little Ruby Book":http://www.humblelittlerubybook.com
-* "Programming Ruby":http://www.ruby-doc.org/docs/ProgrammingRuby/
-* "Why's (Poignant) Guide to Ruby":http://mislav.uniqpath.com/poignant-guide/
-
-同样,你可以在 "rails github":https://github.com/rails/rails 仓库找到这个教程的示例代码,本教程在(rails/railties/guides/code/getting_started)。
-
-h3. Rails 是什么
-
-TIP: 这个章节开始就 Rails 框架的背景和来源做一个详细的描述。你可以安全的跳过这个章节并且在以后再回过头来看它。第三节你会开始你的的 Rails 之旅上的第一个 Rails 应用程序。
-
-Rails是一个使用 Ruby 语言编写的的 web 框架应用程序。其设计目的是为了让每个着手开始编写 web 应用程序的开发人员更加容易的完成工作。它允许你写最少的代码完成超过其他任何语言和框架所完成的工作。经验丰富的 Rails 开发人员还告诉我们通过 Rails 使设计 web 应用程序更快乐。
-
-Rails 是一个值得骄傲的软件。它使得我们以最好的方式去做事情,它也鼓励这种方式——并在某些情况下,鼓励替代的思想,如果你学习 “Rails 方式” 你将会适时的发现生产力的巨大增长。如果你固守在来自其他语言的旧的习惯去进行你的 Rails 开发,以以它地方学来的模式尝试 Rails,那么你将会得到很少的快乐的经历。
-
-Rails 理念包含几个指导原则
-
-* DRY - 不要自己重复——建议一次又一次编写同样的代码是一件坏事请
-* 约定优于配置——意思是 Rails 假设对于你想做什么以及你想怎么做的事情,刻意的做很少的事情比编写无尽的配置文件更好
-
-h4. MVC 架构
-
-Model,View,Controller架构是 Rails 的核心,通常称之为 MVC。MVC 的优点如下:
-* 用户界面与业务逻辑的分离
-* 很容易使得代码保持 DRY # “Don’t Repeat Yourself”
-* 明确代码的不同之处使之跟容易维护
-
-h5. 模型( Models )
-
-模型代表了应用程序的信息(数据)和操纵这些数据的规则。在 Rails 中, models 主要用于管理数据表和相应的规则的互动。在大多数情况,在你数据库中的每个表都会和你的应用程序互动。你应用程序的逻辑业务将会集中放置在 models 中。
-
-h5. 视图( Views )
-
-View代表了应用程序的用户界面。在 Rails 中,Views 通常是嵌入了执行演示数据任务的Ruby代码的HTML文件。Views完成了给web浏览器或者其他工具用于提出来自你的程序的请求提供数据。
-
-h5. 控制器( Controllers )
-
-Controllers 提供了 Models 和 Views 间的粘合。在 Rails 中,Controllers 响应来自 Web 浏览器请求的进程,向 Models 询问数据并将数据传递给 Views 呈现。
-
-h3. Rails 的组件
-
-Rails 关联着许多独立的组件。对这些组件在下面给出简要的解释。如果你是 Rails 的新人,当你阅读到这个部分时,不要忽视任何一个组件的描述,但也没有必要了解的过于深入。例如,我们会构造应用骨架,但你不需要了解有关构造应用骨架的深层知识。
-
-* Action Pack
- ** Action Controller
- ** Action Dispatch
- ** Action View
-* Action Mailer
-* Active Model
-* Active Record
-* Active Resource
-* Active Support
-* Railties
-
-h4. Action Pack
-
-Action Pack 是一个单独的包它包含了 Action Controller, Action View and Action Dispatch(传输调度)。是 MVC 的 VC 部分。
-
-h5. Action Controller
-
-Action Controller 是在 Rails 中管理控制的组件。 The Action Controller 框架进程收到 Rails 应用程序的请求,提取参数,以及调度他们到具有相应义务的动作。这些服务是由 Action Controller 提供,包含会话管理、模板渲染、重定向功能。
-
-h5. Action View
-
-Action View 管理你的 Rails 应用程序的视图。它可以创建 HTML 和 XML 作为默认输出。Action View 管理模板渲染,包含嵌套和局部模板,以及内置的 AJAX 支持。View templates 更多的更详细的内容在 "Layouts and Rendering":http://guides.rubyonrails.org/layouts_and_rendering.html 被提及。
-
-h5. Action Dispatch
-
-Action Dispatch 处理了你和其他部分的应用程序的路由——一些Web请求以及你的配置。Rack applications 是更高级的模块,你可以到 "Rails on Rack":http://guides.rubyonrails.org/rails_on_rack.html 了解它。
-
-h5. Action Mailer
-
-Action Mailer 是一个营造 E-mail 服务的框架。你可以使用 Action Mailer 去发送、接收 Email 。
-
-h5. Active Model
-
-Active Model 提供了 Action Pack gem 服务和 Object Relationship Mapping gems 之间的接口定义,比如 Active 记录。Active Model 允许 Rails 在 Active Record 部分采用其他 ORM 框架如果你的应用程序需要。
-
-h5. Active Record
-
-Active Record 是一个 Rails 应用程序的 Models 根本。它提供有 CRUD 功能独立的数据库,有高级的查找、与另一个 Models 关联的能力,支持几乎所有数据库服务。
-
-h5. Active Resource
-
-Active Resource 提供一个管理目标业务和 RESTful web 服务之间连接的框架。它实现了使用 CRUD 语义测绘 web-base 资源到本地目标。
-
-h5. Active Support
-
-Active Support 是一个广泛收集实用工具类和标准的 Ruby 库的扩展,它们由的核心代码和您的应用程序决定。
-
-h5. Railties
-
-Railties 是在 Rails 代码中创建新 Rails 应用以及在任何 Rails 应用中把粘和各种插件在一起的核心。
-
-h3. REST
-
-Rest作为具有代表性的状态传输是 RESTful 架构的基础。普遍认为它来自 Roy Fielding’s doctoral 的博士论文 《"Architectural Styles and the Design of Network-based Software Architectures":http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm》 当你阅读这篇论文的时候,(可以发现)REST 在 Rails 下面可以归纳为下面的主要原则:
-
-* 使用资源标识符比如 URLs 去表现资源
-* Transferring representations of the state of that resource between system components.在系统组件之间转移(传送)资源的状态
-
-例如,下面的 HTTP 请求:
-
-DELETE /photos/14
-
-(系统)将会明白参照 ID 为 14 的 phone 资源,注明删除该资源。 REST 的自然风格去架构 web 应用程序, Rails 通过这样的钩子,使你避免了许多复杂的 RESTful 和浏览器之间的差异。
-
-如果你想要了解更多关于 REST 架构风格, 以下这些资源可能比 Fielding 的论文更平易近人一些。
-
-* "A Brief Introduction to REST":http://www.infoq.com/articles/rest-introduction by Stefan Tilkov
-
-* "An Introduction to REST":http://bitworking.org/news/373/An-Introduction-to-REST (video tutorial) by Joe Gregorio
-
-* "Representational State Transfer":http://en.wikipedia.org/wiki/Representational_State_Transfer article in Wikipedia
-
-* "How to GET a Cup of Coffee":http://www.infoq.com/articles/webber-rest-workflow by Jim Webber, Savas Parastatidis & Ian Robinson
-
-h3. 建立一个新的 Rails 应用程序
-
-学习(使用)本指导的最好方式是跟随这里描述的每一步,不写代码或者没有例子所需的步骤会使得这个例子被冷落,你可以根据文字描述的步骤一步接着一步的操作。如果你需要完整的代码你可以从这里下载 "Getting Started Code":https://github.com/lifo/docrails/tree/master/guides/code/getting_started 。
-
-如果你跟随这个指导,你将会创建一个叫做 blog 的 Rails 项目——一个非常简单的网络博客。当你准备开始构建这个项目之前你需要确保 Rails 已经完全安装。
-
-TIP: 下面例子中使用 `#` 和 `$` 来表示 终端提示。如果你正在使用 Windows 提示可能会这样 `c:\source_code>`
-
-h4. 安装 Rails
-
-通过方便的 RubyGems 提供的 +gem install+,安装 Rails:
-
-
-$ gem install rails
-
-
-TIP: 如果你在 Window 下面工作,你可以使用 "Rails Installer":http://railsinstaller.org 快速安装 Ruby and Rails。
-
-确认所有的(依赖)安装正确,你应该运行如下命令:
-
-
-$ rails --version
-
-
-如果终端提示类似这样的文字 "Rails 3.2.2" 那么你已经准备好继续了。
-
-h4. Creating the Blog Application
-
-开始,打开一个 terminal,导航至一个你有权限创建文件的文件夹,并输入:
-
-
-$ rails new blog #--skip-bundle # Don't run bundle install这样在国内就不会由于连不上gem即便上能够上也会很慢半天没反映 可以尝试使用 淘宝gem镜像
-
-
-这里将创建一个名叫 Blog 的 Rails 应用程序在名称为 blog 的目录中。
-
-TIP: 你可以通过运行 rails new -h,查看 Rails 应用程序创建器的所有命令(开关)。
-
-当你创建了这个 blog 程序,跳转到它所在的文件夹中(直接对这个程序编辑).
-
-
-$ cd blog
-
-
-命令 +rails new blog+ 将会创建一个名为 blog 的文件夹在你的工作目录中。目录 blog 包含一些自动生成的文件它构成了 Rails 应用程序的结构。在这个体验中的大多数的工作都是在 app/ 这个文件夹中完成的,这里对 Rails 默认创建的每一个文件和文件夹的功能做出了一个概述:
-
-|_.File/Folder|_.Purpose|
-|app/|包含 controllers, models, views 和 你应用程序的 assets(资源),再接下面的手册中你主要的注意力应该放在这里。|
-|config/|配置你的应用程序的运行的规则,(url)路由,数据库和其他,更多的信息查看 "Configuring Rails Applications":http://guides.rubyonrails.org/configuring.html|
-|config.ru|基于 Rack 服务器使用这个应用程序的 Rack 配置用于开始应用程序(Rack configuration for Rack based servers used to start the application)|
-|db/|显示你当前的数据库结构(database schema),同样也显示数据迁移。|
-|doc/|应用程序的(深入)全面的文档。|
-|Gemfile
Gemfile.lock|这个文件让你可以(添加)你的 Rails 所需要的特殊的 Gem 依赖关系。这个文件被 Bundler gem 使用,更多的信息查看 "the Bundler website":http://gembundler.com |
-|lib/|应用程序用到的扩展库(本手册没有涉及)|
-|log/|应用程序的日志文件|
-|public/|这是外部可见的唯一文件夹。包含静态文件和编译资源。|
-|Rakefile|这个文件定位和载入能够在命令行中运行的任务。这个任务定义贯穿整个 Rails 的组件。除了修改 Rakefile,你更应该添加你自己的任务的文件到你的应用程序的 lib/tasks 目录。|
-|README.rdoc|这是一个简单的说明手册。你需要编辑这个文件告诉其他人你的应用程序可以做什么,怎么安装等等。|
-|script/|包含运行你的 app 的 rails 脚本,或者其他用来配置或运行你的应用程序的 scripts。|
-|test/|单元测试, fixtures,或者其他 test 工具。他们在 "Testing Rails Applications":http://guides.rubyonrails.org/testing.html 里面有完整的讲述。|
-|tmp/|临时文件|
-|vendor/|放置第三方代码的地方。在一个典型的 Rails 应用程序中,这里包含 Ruby Gems,Rails 源代码(如果你把 Rails 安装到你的项目中)还包含一些预先包装好的额外的插件|
-
-h3. 配置一个数据库
-
-几乎每个 Rails 应用程序都会和一个数据库交互。使用的数据库在一个配置文件 +config/database.yml+ 中被指定。如果你打开一个在新的 Rails 应用程序的中的配置文件,你将会看到默认的数据库被配置使用 SQLite3。这个文件包含 Rails 默认能够运行的三个不同环境的部分:
-
- * development 环境被使用在你的开发/本地计算机作为你与应用程序的手动交互。
- * test 环境被使用来运行自动测试。
- * production 环境在你部署你的应用程序给所有人使用的时候使用。
-
-TIP: 你不需要必须手动的更新数据库配置。如果你查看应用程序创建器的选项,你将会发现其中一个选项叫—— database。这个选项允许你从最常使用的关系数据库列表中选择一个适配器。你甚至可以反复运行创建器命令: +cd .. && rails new blog —database=mysql+。当你确认重写 +config/database.yml+ 文件,你的应用程序将会被配置为 MySQL 替代 SQLite。这些一般数据库连接的详细的例子在下面。
-
-h4. 配置一个 SQLite3 数据库
-
-Rails 内置支持 "SQLite":http://www.sqlite.org/ ,这是一个轻量级的非服务器,(serverless)数据库应用程序。即使一个繁忙的产品环境可能会超出 SQLite (所能),它在开发和测试环境能够很好的工作。Rails 在创建一个新的项目的时候默认使用一个 SQLite 数据库,但是你总是可以在以后改变它。
-
-这里是一个开发环境(数据库)连接信息默认配置文件(+config/database.yml+)的节选:
-
-
-development:
- adapter: sqlite3
- database: db/development.sqlite3
- pool: 5
- timeout: 5000
-
-
-TIP: 在这个教程中我们使用一个 SQLite3 数据库来存储数据,因为它是一个零配置数据库其就可以工作。Rails 同样也支持 MySQL 和 PostgreSQL “开箱即用的”,以及很多数据库系统的插件。如果你正在产品环境中使用一个数据库大多数情况下 Rails 都有一个与之相应的插件。
-
-h4. 配置一个 MySQL 数据库
-
-如果你选择使用一个 MySQL 替代(项目初始化)附带的 SQLite3 数据库,你的 +config/database.yml+ 将会看起来有些不同。这里是 development 部分:
-
-
-development:
- adapter: mysql2
- encoding: utf8
- database: blog_development
- pool: 5
- username: root
- password:
- socket: /tmp/mysql.sock
-
-
-如果你的开发电脑的 MySQL 安装的时候包含了一个名叫 'root' 的用户以及一个空密码,这个配置文件应该会为你工作。否则,在 development 部分模式更改为合适的用户名和密码。
-
-h4. 配置一个 PostgreSQL 数据库
-
-如果你选择使用 PostgreSQL,你的 +config/database.yml+ 将会定制来使用 PostgreSQL 数据库:
-
-
-development:
- adapter: postgresql
- encoding: unicode
- database: blog_development
- pool: 5
- username: blog
- password:
-
-
-h4. 配置一个用于 JRuby 平台的 SQLite3 数据库
-
-如果你选择使用 SQLite3 并且使用 JRuby,你的 +config/database.yml+ 将会看起来有点不同。这里是 development 部分:
-
-
-development:
- adapter: jdbcmysql
- database: blog_development
- username: root
- password:
-
-
-h4. 配置一个用于 JRuby 平台的 PostgreSQL 数据库
-
-最后如果你选择使用 PostgreSQL 并且正在使用 JRuby,你的 +config/database.yml+将会看起来有点不同。这里是 development 部分:
-
-
-development:
- adapter: jdbcpostgresql
- encoding: unicode
- database: blog_development
- username: blog
- password:
-
-
-
-h3. Hello, Rails!
-
-传统的方式之一,开始使用一种新的(命令)语法并得到快速掠过的文字。要得到这样的结果你需要使你的 Rails 程序运行。
-
-h4. 启动web服务
-
-你实际上已经有了一个 Rails 功能的应用程序了。看一看它,你需要在你的生产机器上启动一个 web 服务,你可以这样来启动。
-
-
-$ rails server
-
-
-TIP: 编译 CoffeeScript 到 JavaScript需要一个 JavaScript 运行库,确实一个运行库将会给出一个 +execjs+ error。通常 Mac OS X 和 Windows 自带了一个 JavaScript 运行库。Rails 添加了 +therubyracer+ gem 到 Gemfile 的注释行中如果你需要可以取消对它的注释。+therubyrhino+ 推荐 JRuby 使用并且默认添加到 Gemfile 中如果应用程序在 JRuby 中创建。你可以查看所有的支持的运行库 "ExecJS":https://github.com/sstephenson/execjs#readme。
-
-这里默认将开启一个 WEBrick 服务器的的实例(Rails 也可能使用一些其他的 web 服务器)。查看你的应用程序的行为,打开一个浏览器并且导航到 127.0.0.0:3000 你将会看到一个 Rails 默认的信息页面。
-
-!images/rails_welcome.png(Welcome Aboard screenshot)!
-
-TIP: 要终止 web 服务,在命令运行的终端中按下 Ctrl+C 。在开发环境模式中,Rails 一般不需要你停止服务;你所做的更改将自动的编译进需要的文件中并且重启服务。
-
-这个欢迎界面体现了一个新的 Rails 应用程序创建成功(通过了 Rails 的自检)。你可以点击‘ About your application’s environment’查看你的应用程序运行环境摘要信息。
-
-h4. Rails 说 Hello"
-
-要使 Rails 说出(显示)“你好”,你还需要创建一个最小的 controller 和 view。幸运的是,你可以完成这些通过一行命令。在终端中输入:
-
-
-$ rails generate controller welcome index
-
-
-提示: 如果你在输入这个命令的时候出现没有这个命令错误,你需要明确的使用 ruby 来执行 Rails 命令。
-
-Rails 将会为你创建一些文件,包含 `app/views/home/index.html.erb` 。这个模板会用来显示在 home controller 中的 index action (method) 的结果。在文本编辑器中打开这个文件并输入:
-
-
-Hello, Rails!
-
-
-h4. 设置应用程序主页
-
-现在我们已经创建了 controller 和 view,我们还需要告诉 Rails 我们想在什么时候显示出来。在本例中,我们想让它在我们导航至站点 url 根目录 "http://localhost:3000":http://localhost:3000 的时候替代 “Welcome Aboard” 显示。
-
-首先移除应用程序中的默认页面。
-
-
-$ rm public/index.html
-
-
-我们必须这样做因为,Rails将会传送任何在public的静态文件优先于我们在 controllers生成的动态(显示)内容。
-
-现在你还必须告诉 Rails 你实际上的主页在哪里。在文本编辑器中打开 +config/routes.rb+ 。这是你应用程序的路由文件,它采用 DSL 语言囊括了告诉 Rails 怎样连接请求信息到 controllers 和 actions的所有条目。这个文件包含许多简单的路由命令,其中一条实际上是用于告诉我们怎样连接你站点根目录到一个指定的controller and acti。找到以root :to开头的那一行,注释掉它改成如下内容:
-
-
-Blog::Application.routes.draw do
-
- #...
- # You can have the root of your site routed with "root"
- # just remember to delete public/index.html.
- root :to => "welcome#index"
-
-
-+root :to => "welcome#index"+告诉 Rails 映射请求到应用程序的root action到 home 控制器的 index action。
-
-现在你在浏览器中导航至 "http://localhost:3000":http://localhost:3000 ,你将会看到“Hello, Rails!”.
-
-NOTE. 更多的信息请参见 "Rails Routing from the Outside In":routing.html.
-
-h3. 使用 Scaffolding 快速创建并运行
-
-是一个快速的方法产生应用程序的一些重要组成。如果你想使用一种简单的操作为新资源创建models,views和controllers,Scaffolding是一个不错的工具。
-
-h3. 创建一个资源
-
-在本示例中的 blog 应用程序,你可以使用 scaffolded 产生 post 资源:它表现为一个简单的 blog posting。要完成这些,在终端输入如下命令:
-
-
-rails generate scaffold Post name:string title:string content:text
-
-
-创建器将会在应用程序中的一些文件夹中生成一些文件,并且还会编辑 +config/routes.rb+。下面这些产生的文件的大概说明:
-
-|_.File/Folder|_.Purpose|
-|db/migrate/20100207214725_create_posts.rb |将创建的posts表单迁移到你的数据库(会在你的命名前面加上时间)|
-|app/models/post.rb |Post模型|
-|test/unit/post_test.rb |Unit testing harness for the posts model|
-|test/fixtures/posts.yml |模拟测试post|
-|config/routes.rb |Edited to include routing information for posts|
-|app/controllers/posts_controller.rb|The Posts controller|
-|app/views/posts/index.html.erb|一个显示所有post的视图|
-|app/views/posts/edit.html.erb |一个编辑post的视图|
-|app/views/posts/show.html.erb |一个显示一条post的视图|
-|app/views/posts/new.html.erb |一个创建post的视图|
-|app/views/posts/_form.html.erb |一个局部用于控制编辑和创建新视图的整体视效的表单|
-|test/functional/posts_controller_test.rb|Functional testing harness for the posts controller|
-|app/helpers/posts_helper.rb |使用post的helper|
-|test/unit/helpers/posts_helper_test.rb |Unit testing harness for the posts helper|
-|app/assets/javascripts/posts.js.coffee |CoffeeScript for the posts controller|
-|app/assets/stylesheets/posts.css.scss|Cascading style sheet for the posts controller|
-|app/assets/stylesheets/scaffolds.css.scss |Cascading style sheet#层叠样式 to make the scaffolded views look better|
-
-NOTE. 即便是scaffolding使你创建和运行非常快捷,但是产生的代码不可能完美的适合你的应用程序。你大多数都需要定制产生的代码。很多有经验的Rails开发人员完全不使用scaffolding,宁愿从头编写全部的代码。Rails,无论如何,使得为生成的models,controllers,views或者其他代码编定制模板非常简单。你可以在 "Creating and Customizing Rails Generators & Templates":http://guides.rubyonrails.org/generators.html 看到更多信息。
-
-h4. 执行数据迁移
-
-+rails generate scaffold+ 命令的一个产物就是数据迁移。Migrations是一个ruby类被设计用来使数据库表单的创建和修改变得简单。Rails使用rake命令来执行迁移,它还可以撤销已经应用的修改。 迁移文件名包含了一个时间戳确保了迁移能够完成。
-
-如果你查看 +db/migrate/20100207214725_create_posts.rb+ 这个文件(记住,你得到的可能会有略微不同),你将会发现:
-
-
-class CreatePosts < ActiveRecord::Migration
- def change
- create_table :posts do |t|
- t.string :name
- t.string :title
- t.text :content
-
- t.timestamps
- end
- end
-end
-
-
-整个 migration 创建了一个名叫 change 的方法,该方法在你运行这个 migration 的时候被调用。这个方法中定义的行为也是可逆的,那就是说 Rails 知道怎样逆向改变这个migration,如果你需要恢复到上一次数据。默认情况下,当你运行这个 migration,他将会创建一个包含两个字符串列和一个 text 列的表单。关于 Rails migration 的更多信息请阅读 "Rails Database Migrations":http://guides.rubyonrails.org/migrations.html 手册。
-
-这个时候,你可以使用 rake 命令运行 migration 了:
-
-
-rake db:migrate
-
-
-Rails 将会执行这个 migration 命令并且通知你它创建了 Post 表单。
-
-
-== CreatePosts: migrating ====================================================
--- create_table(:posts)
- -> 0.0019s
-== CreatePosts: migrated (0.0020s) ===========================================
-
-
-TIP: 由于你默认工作在开发环境中,这个命令将会应用于开发环境会话的数据库位于你的+config/database.yml+ 中。如果你想执行 migration 在其他环境中,比如以产品(环境)为实例,你必须明确调用的通过命令行中执行:+rake db:migrate RAILS_ENV=production+。
-
-h4. 添加一个 Link
-
-你已经创建好的 post 挂到主页上,你可以通过添加一个 link 到主页。打开 +app/views/home/index.html.erb+ 并且按照下面所示更改:
-
-
-Hello, Rails!
-<%= link_to "My Blog", posts_path %>
-
-
-这个链接方法是 Rails 在 view helpers 的内建方法之一 。它创建一个基于文字的超级链接并显示到哪里,在这个实例中(跳转)到 posts。
-
-h4. Working with Posts in the Browser
-
-现在你已经准备好在 posts 中工作了。导航至 "http://localhost:3000":http://localhost:3000,并且点击 “My Blog” 链接。
-
-!images/posts_index.png(Posts Index screenshot)!
-
-这就是 Rails 渲染你的 posts 视图后的结果。在你点击 “New Post” 链接并创建一个新的 post 之前,数据库里面是没有任何 post 的。随后你可以编辑,查看详细内容,或者删除他们。post 的所有的 logic 和 HTML 都是通过 +rails generate scaffold+ 命令生成的。
-
-TIP: 在开发模式中(你的默认工作模式),Rails 会在每个浏览器请求的时候重新载入你的应用程序,因此你不需要停止或者重启 web 服务。
-
-恭喜,你已经驯服了 rails!现在是时候去看看它的所有工作了
-
-h4. The Model
-
-The model file, +app/models/post.rb+ is about as simple as it can get:
-
-
-class Post < ActiveRecord::Base
- attr_accessible :content, :name, :title
-end
-
-
-这里有可能不一致——但是注意 +Post+ 类继承于 +ActiveRecord::Base+。Active Record 免费为你的 models 提供了强大的功能,包括基本数据库的 CRUD(创建,读取,更新,删除)操作,数据验证,以及复杂的的查询与其它数据表单多关联的字段的支持能力。
-
-h4. 添加一些验证
-
-Rails 包含一些帮助你验证发送到models的数据的方法。打开 +app/models/post.rb+ 并编辑:
-
-
-class Post < ActiveRecord::Base
- attr_accessible :content, :name, :title
- validates :name, :presence => true
- validates :title, :presence => true,
- :length => { :minimum => 5 }
-end
-
-
-这些更改会确保所有的 post 都有一个 name 和 titile 并且 title 长度至少五个字符。Rails 可以验证很多种字段,比如字段能否为空和独特性,字段的格式,以及字段的关联。验证详细描述在 "Active Record Validations and Callbacks":active_record_validations_callbacks.html#validations-overview
-
-h4. 使用控制台
-
-要想在action里面查看你的验证你可以使用 console。console 是一个可以让你在你的应用程序的上下文中执行 Ruby 代码的命令行工具:
-
-
-$ rails console
-
-
-TIP: 默认的 console 将会改变你的数据库。你可以通过运行 rails console
---sandbox,这样你可以(在退出控制台后)回滚你的任何操作
-
-在载入控制台后,你可以使用它来对你应用程序的models进行工作:
-
-
->> p = Post.new(:content => "A new post")
-=> #
->> p.save
-=> false
->> p.errors.full_messages
-=> ["Name can't be blank", "Title can't be blank", "Title is too short (minimum is 5 characters)"]
-
-
-这段代码演示了创建一个 +Post+ 实例,并企图保存到数据库并得到一个 +false+ 的返回值(说明保存失败的原因),检查 post 的错误信息。
-
-当你操作完成,输入 +exit+ 并回车退出 console。
-
-TIP: 不像开发环境的 web 服务器 console 不会自动导入你每行输入的新的代码。如果你改变了你的 models 并且 console 是打开的,输入 +reload!+ 那么 console 会立即导入他们。
-
-h4. Listing All Posts
-
-Let's dive into the Rails code a little deeper to see how the application is
-showing us the list of Posts.让我们深入 Rails 代码一点点,去看看程序是怎样展示 post 列表给我们的。打开文件 +app/controllers/posts_controller.rb+ 并且查看 +index+ action:
-
-
-def index
- @posts = Post.all
-
- respond_to do |format|
- format.html # index.html.erb
- format.json { render :json => @posts }
- end
-end
-
-
-+Post.all+ 调用 Post model 并返回当前在数据库中的所有 post 为一个 +Post+ 记录的数组。并且我们将这个数组存储在一个叫做@posts的实例变量中。
-
-TIP: 有关 Active Record 更多的信息,可以查看 "Active
-Record Query Interface":active_record_querying.html 相关记录。
-
-这个 +respond_to+ 块处理了这个动作的 HTML 和 JSON 请求。如果你浏览 "http://localhost:3000/posts.json":http://localhost:3000/posts.json,你将会看到一个 JSON 包含着所有的 post。这个 HTML 格式在+app/views/posts/+ 的 view 中查找相对应的动作名称。Rails 使来自 action 的所有的(可用的)实例变量对应到view。
-
-
-Listing posts
-
-
-
- | Name |
- Title |
- Content |
- |
- |
- |
-
-
-<% @posts.each do |post| %>
-
- | <%= post.name %> |
- <%= post.title %> |
- <%= post.content %> |
- <%= link_to 'Show', post %> |
- <%= link_to 'Edit', edit_post_path(post) %> |
- <%= link_to 'Destroy', post, :confirm => 'Are you sure?',
- :method => :delete %> |
-
-<% end %>
-
-
-
-
-<%= link_to 'New post', new_post_path %>
-
-
-这个 view 迭代 +@posts+ 数组所有的内容并显示相关的内容和链接。关于视图备注一些信息:
-
-* +link_to+ 创建一个超链接到一个地方
-* +edit_post_path+ 和 +new_post_path+ 是 Rails 提供的 RESTful 路由向导。你将会在不同 controller 看到一系列的不同的 actions helpers。
-
-NOTE: 在以前的版本的 Rails 中,你必须使用 +<%=h post.name %>+ 以避免一些HTML可能会在插入到页面之前转义。在 Rails 3.0,作为默认。得到一个非转义的 HTML,你现在使用 <%= raw post.name %>。
-
-TIP: 了解更过关于渲染处理流程,阅读 "Layouts and Rendering in
-Rails":layouts_and_rendering.html.
-
-h4. 定制布局
-
-view 仅仅告诉 HTML 在你的 web 浏览器里面要显示什么(内容)。Rails 也有关于 +layouts+ 的概念(定义),那就是布局是对 view 的包装。当 Rails 渲染一个 view 到浏览器,它通常是这样做: 把 view 的 HTML 放进布局的 HTML 中。在以前的版本中,+rails generate scaffold+ 命令将会自动创建 controller 对应指定的布局,就像 +app/views/layouts/posts.html.erb+ 对应于 posts controller。 然而在 rails3.0中有所不同了。一个应用程序指定的布局适用于所有的 controllers,可以在 +app/views/layouts/application.html.erb+ 中找到(这就好像是django的base.html)。打开这个 layout 在你的编辑器中并且修改 +body+ 标签:
-
-
-
-
-
- Blog
- <%= stylesheet_link_tag "application" %>
- <%= javascript_include_tag "application" %>
- <%= csrf_meta_tags %>
-
-
- <%= yield %>
-
-
-
-
-
-
-现在你刷新 +/posts+ 页面,你将会看到一个灰色的页面背景。并且相同的灰色背景将会使用在 posts 的所有视图中。
-
-h4. 建立新文章
-
-创建一个 new post 包含两个 actions。首先是 +new+ action,它会实例化一个空的 +Post+ 对象:
-
-
-def new
- @post = Post.new
-
- respond_to do |format|
- format.html # new.html.erb
- format.json { render :json => @post }
- end
-end
-
-
-这个 +new.html.erb+ 视图显示这个空的 post 给用户:
-
-
-New post
-
-<%= render 'form' %>
-
-<%= link_to 'Back', posts_path %>
-
-
-+<%= render 'form' %>+ 是我们第一个介绍的 Rails 的 partials。一个 partial 是一个 HTML 代码片段和 Ruby 代码的组合可以在多目标对象中重用。(类似于django的include 'other.html')。
-在本例中, form 用于创建 new post,它相当于一个用于编辑一个 post 的表单,这个表单有一个 name text fields 和 title text fields 以及一个 content 的 text area 还有一个用于添加一个新的 post 或者更新已经存在的 post 的按钮。
-
-如果你看一下 +views/posts/_form.html.erb+ 这个文件,你将会发现下面的内容:
-
-
-<%= form_for(@post) do |f| %>
- <% if @post.errors.any? %>
-
-
<%= pluralize(@post.errors.count, "error") %> prohibited
- this post from being saved:
-
- <% @post.errors.full_messages.each do |msg| %>
- - <%= msg %>
- <% end %>
-
-
- <% end %>
-
-
- <%= f.label :name %>
- <%= f.text_field :name %>
-
-
- <%= f.label :title %>
- <%= f.text_field :title %>
-
-
- <%= f.label :content %>
- <%= f.text_area :content %>
-
-
- <%= f.submit %>
-
-<% end %>
-
-
-这个 partial 接收在 view 文件中定义的所有的实例变量。因此在本例中,controller 申请新的 Post 对象给@post,@post 在 view 和 partial 都是可用的。
-
-有关 partials 的更多信息,参考 "Layouts and Rendering in
-Rails":layouts_and_rendering.html#using-partials guide 指导。
-
-+form_for+ 代码块用于创建一个HTML表单。在这个代码块中你可以在访问方法的基础上在表单上创建各种控制。比如,+f.text_field :name+ 告诉 Rails 在表单中创建一个 text input 并且对应于显示实例的 +name+ 属性。form 使用的方法基于 model 的相对应的字段属性(类型如 text_field 或 text_area)(例如本例中的 +name+, +title+, and +content+)。Rails 偏好于使用(偏向于使用)+form_for+ 列出你要输入的 HTML 行因为这样代码更加简洁,并且这样使得 form 和 particular model 实例关系更加明显。
-
-+form_for+ 代码块同样也足够你定制你的 _New Post_ 和 _Edit Post_ action,并且将会设置 form +action+ 标签以及在 HTML 输出中显示的提交按钮的名称。
-
-TIP: 如果你需要创建一个 HTML 表单显示任意的域,而不绑定到 model 字段中,你应该使用 +form_tag+ 方法,它快捷的保证了建立 forms 不必在绑定到一个 model 实例。
-
-当用户点击这张表单上面的 +Create Post+ 按钮,浏览器将会发回信息到controller的 +create+ action(Rails知道调用 +create+ 方法,因为form是以HTTP POST请求发送,这是我随后提到的一种协议之一):
-
-
-def create
- @post = Post.new(params[:post])
-
- respond_to do |format|
- if @post.save
- format.html { redirect_to(@post,
- :notice => 'Post was successfully created.') }
- format.json { render :json => @post,
- :status => :created, :location => @post }
- else
- format.html { render :action => "new" }
- format.json { render :json => @post.errors,
- :status => :unprocessable_entity }
- end
- end
-end
-
-
-+create+ action 实例化一个新的 Post 对象,这个对象给 form 提供数据支持,Rails 会生成有效的 +params+ hash。当成功的保存了新 post,+create+ 返回用户请求的适当的格式(在本例中是 HTML)。然后重定向用户页面到结果显示的 post show action页面并且给出提示 Post 成功的创建了。
-
-如果 post 没有保存成功,是因为(数据)验证错误,然后 controller 控制用户页面回到 +new+ action(包含验证错误新息)给用户,以便用户更改错误并重新提交。
-
- “Post was successfully created.” 这条消息被存储在 Rails 的 flash 的 hash 表中,(通常之叫它 _the flash_ )因此消息可以转载到另一个 action,在请求状态中提供有用的信息给用户。在这个新建例子(数据验证失败)中,用户实际上从来不看任何在页面创建进程中的渲染页面,因为它立刻重定向页面到 new Post当 Rails 保存了这个记录。”Flash装载消息到接下来的 action,因此当用户被重定向到了 show action,他们立刻收到了一条消息“Post was successfully created.”。
-
-h4. 显示一条 Post
-
-当你在posts 的主页面点击一个 post 的 +show+ 这个超链接,他将会产生一个 url: +http://localhost:3000/posts/1+。Rails解释这是一个resource的+show+ action 调用。这里是 +show+ action:
-
-
-def show
- @post = Post.find(params[:id])
-
- respond_to do |format|
- format.html # show.html.erb
- format.json { render :json => @post }
- end
-end
-
-
-这里的 +show+ action使用 +Post.find+ 通过对应记录的id来查找单个记录。当找到记录,Rails使用 +app/views/posts/show.html.erb+ 来显示它:
-
-
-<%= notice %>
-
-
- Name:
- <%= @post.name %>
-
-
-
- Title:
- <%= @post.title %>
-
-
-
- Content:
- <%= @post.content %>
-
-
-
-<%= link_to 'Edit', edit_post_path(@post) %> |
-<%= link_to 'Back', posts_path %>
-
-
-h4. 编辑Posts
-
-类似创建一个新的 post,编辑一个 post 分为两部分。首先是到 +edit_post_path(@post)+ 请求一个特定的 post。这里是调用的在 controller 中的 +edit+ action:
-
-
-def edit
- @post = Post.find(params[:id])
-end
-
-
-再找到了请求的 post, Rails 试图使用 +edit.html.erb+ 来显示它:
-
-
-Editing post
-
-<%= render 'form' %>
-
-<%= link_to 'Show', @post %> |
-<%= link_to 'Back', posts_path %>
-
-
-再一次的,就像 +new+ action,+edit+ action也使用 +form+ partial,这次有所不同,form将会提交一个PUT action到 +PostsController+ 并且提交按钮将会显示为 “Update Post”。
-
-提交的form由上面这个视图创建的并且还会调用 controller 中的 +update+ action:
-
-
-def update
- @post = Post.find(params[:id])
-
- respond_to do |format|
- if @post.update_attributes(params[:post])
- format.html { redirect_to(@post,
- :notice => 'Post was successfully updated.') }
- format.json { head :no_content }
- else
- format.html { render :action => "edit" }
- format.json { render :json => @post.errors,
- :status => :unprocessable_entity }
- end
- end
-end
-
-
-在 +update+ action中,Rails 首先使用 +:id+ 参数从 edit view(传值到)数据库记录下刚才编辑的内容。当获取了请求的 +post+ parameter(一个hash字典)就会 call +update_attributes+ 并且 应用 hash 字典的值到相应的记录。如果一切成功,用户会被重定向到 post 的 show 视图。如果期间发生了任何错误,它将回到 edit 视图并(要求)改正他们。
-
-h4. 删除一个post
-
-最后,点击一个 +destroy+ 链接发送相关的id到 +destroy+ 动作:
-
-
-def destroy
- @post = Post.find(params[:id])
- @post.destroy
-
- respond_to do |format|
- format.html { redirect_to posts_url }
- format.json { head :no_content }
- end
-end
-
-
-这个 +destroy+ 是 Active Recordmodel 的实例(功能是)从数据库中移除相应的记录。当这个(操作)完成,这里没有任何记录显示,因此 Rails 重定向用户的浏览器到 model 的index view。
-
-h3. 添加第二个Model(comment)
-
-你已经知道了通过scaffolding生成的model看起来是怎样的。第二个model用来处理blog post的评论。
-
-h4. 构造一个model
-
-Rails 中的 Models 使用一个单数名称,同时它们相关的数据库表使用一个复数名称。对于评论在 models 中的代名词,习惯上使用的的是 +Comment+。即使你不想完完全全的使用 scaffolding,大多数的 Rails 仍然使用生成器来做这些事情比如 models 和 controllers。要创建一个新的 model,在终端中运行下面这条命令:
-
-
-$ rails generate model Comment commenter:string body:text post:references
-
-
-这条命令将会生成四个文件:
-
-|_.File |_.Purpose|
-|db/migrate/20100207235629_create_comments.rb | Migration to create the comments table in your database (your name will include a different timestamp) |
-| app/models/comment.rb | The Comment model |
-| test/unit/comment_test.rb | Unit testing harness for the comments model |
-| test/fixtures/comments.yml | Sample comments for use in testing |
-
-首先,看一看 +comment.rb+:
-
-
-class Comment < ActiveRecord::Base
- belongs_to :post
-end
-
-
-这和你刚刚看到 post.rb 很近似。不同的是这行 belongs_to :post,他会设定一个 Active Record association。你将会在接下来的 guide 学习一点有关 associations 的内容。
-
-除了模型,Rails 同样也产生了一个 migration 来创建相应的数据库表单:
-
-
-class CreateComments < ActiveRecord::Migration
- def change
- create_table :comments do |t|
- t.string :commenter
- t.text :body
- t.references :post
-
- t.timestamps
- end
-
- add_index :comments, :post_id
- end
-end
-
-
-对于 +t.references+ 这行,会在两个 models 之间生成一个外键列从而形成一个关系(组合)。而且 +add_index+ line生成一个索引关联到这个关系行:
-
-
-$ rake db:migrate
-
-
-Rails 能够智能的只针对对没有被运行过的表单,执行 migrations 生成当前的数据库,因此这里你只会看到:
-
-
-== CreateComments: migrating =================================================
--- create_table(:comments)
- -> 0.0008s
--- add_index(:comments, :post_id)
- -> 0.0003s
-== CreateComments: migrated (0.0012s) ========================================
-
-
-h4. Associating Models
-
-Active Record associations让你很容易的申明两个models之间的关系。在本例中的comments和posts,你可以写出这样描述关系:
-
-* 一条comment对应于一个post。
-* 一个post可以对应于多个comments。
-
-实际上,这与接近 Rails 声明的 这个关系的语句语义 。你已经看到了在 Comment model 中的使每个 comment对应于一个 post的代码。
-
-
-class Comment < ActiveRecord::Base
- belongs_to :post
-end
-
-
-你将会需要编辑 +post.rb+ 文件来添加另一半关系。
-
-
-class Post < ActiveRecord::Base
- attr_accessible :content, :name, :title
- validates :name, :presence => true
- validates :title, :presence => true,
- :length => { :minimum => 5 }
-
- has_many :comments
-end
-
-
-These two declarations enable a good bit of automatic behavior. For example, if you have an instance variable +@post+ containing a post, you can retrieve all the comments belonging to that post as an array using +@post.comments+.
-这两个声明启用了一个很好的自动关联功能。例如 如果你有一个包含一 post 的实例变量 +@post+,你可以使用 +@post.comments+ 以数组的形式取回属于这一 post 的所有评论。
-
-TIP: 更多关于Active Record associations的信息查看 "Active Record
-Associations":association_basics.html guide 。
-
-h4. 给Comments添加路由
-
-作为 +welcome+ controller,我们将还需要添加一个路由让 Rails 知道我们导航到哪里可以看到 +comments+。再次打开 +config/routes.rb+ 文件,你将会看到 scaffold 创建器在顶部为 +posts+ 自动添加的入口,resources
-:posts,把它改成如下:
-
-
-resources :posts do
- resources :comments
-end
-
-
-这里把 creates +comments+ 作为一个嵌套资源放在 +posts+ 中。这正是 posts 和comments 的分层关系的表现。
-
-TIP: 关于 routing 的更多的信息,查看 "Rails Routing from the Outside
-In":routing.html guide。
-
-h4. 构造一个 Controller
-
-Model 已经到手了,你可以把你的注意力放到创建一个匹配的 controller 上了。类似的,像这样构造:
-
-
-$ rails generate controller Comments
-
-
-这里新建了6个文件和一个空目录:
-
-|_.File/Directory |_.Purpose |
-| app/controllers/comments_controller.rb | The Comments controller |
-| app/views/comments/ | Views of the controller are stored here |
-| test/functional/comments_controller_test.rb | The functional tests for the controller |
-| app/helpers/comments_helper.rb | A view helper file |
-| test/unit/helpers/comments_helper_test.rb | The unit tests for the helper |
-| app/assets/javascripts/comment.js.coffee | CoffeeScript for the controller |
-| app/assets/stylesheets/comment.css.scss | Cascading style sheet for the controller |
-
-就像大多数 blog,我们的读者将会直接发表他们的评论在他们阅读 post 的时候,并且一旦他们添加评论成功,将会回到 postshow 页面去查看他们刚刚列出的评论。正因为这样(的考虑),我们的 CommentsController如下,它提供一个方法来创建 comments 和删除垃圾评论。
-
-因此首先,我们来到 Post show template(+/app/views/posts/show.html.erb+),创建一个新的 comment:
-
-
-<%= notice %>
-
-
- Name:
- <%= @post.name %>
-
-
-
- Title:
- <%= @post.title %>
-
-
-
- Content:
- <%= @post.content %>
-
-
-Add a comment:
-<%= form_for([@post, @post.comments.build]) do |f| %>
-
- <%= f.label :commenter %>
- <%= f.text_field :commenter %>
-
-
- <%= f.label :body %>
- <%= f.text_area :body %>
-
-
- <%= f.submit %>
-
-<% end %>
-
-<%= link_to 'Edit Post', edit_post_path(@post) %> |
-<%= link_to 'Back to Posts', posts_path %> |
-
-
-This adds a form on the +Post+ show page that creates a new comment by
-calling the +CommentsController+ +create+ action. Let's wire that up:
-这里添加一个forms在Post show页面用来创建一个新的评论,它将会调用 +CommentsController+的 +create+ action,因此让我们补充上下面的内容:
-
-
-class CommentsController < ApplicationController
- def create
- @post = Post.find(params[:post_id])
- @comment = @post.comments.create(params[:comment])
- redirect_to post_path(@post)
- end
-end
-
-
-这里你看到的会比你在 controller 中为 posts 做的要复杂点。那就是你刚刚你刚补充的副作用;每个面向 comment 的请求都保持了它所依附的 post 的踪迹(id),因此这样初始化 +find+ 的时候匹配相应的 +Post+ model(时)得到了答案。
-
-此外,上面的代码带来的好处就是使得一些对于 association 的方法可用。我们使用 +@post.comments+ 中的 +create+ 方法来新建和保存 comment。这里将会自动连接link使得 comment 依附于对应的 post。
-
-一旦我们评论过后,我们使用 +post_path(@post)+ 导引用户到先前的 post。正如我们已经看到的,这里调用 +PostsController+ 的 +show+ action它将返回并渲染show.html.erb 模板。这里也是我们想让 comment 显示的地方,因此让我们添加(那些代码)到 +app/views/posts/show.html.erb+。
-
-
-<%= notice %>
-
-
- Name:
- <%= @post.name %>
-
-
-
- Title:
- <%= @post.title %>
-
-
-
- Content:
- <%= @post.content %>
-
-
-Comments
-<% @post.comments.each do |comment| %>
-
- Commenter:
- <%= comment.commenter %>
-
-
-
- Comment:
- <%= comment.body %>
-
-<% end %>
-
-Add a comment:
-<%= form_for([@post, @post.comments.build]) do |f| %>
-
- <%= f.label :commenter %>
- <%= f.text_field :commenter %>
-
-
- <%= f.label :body %>
- <%= f.text_area :body %>
-
-
- <%= f.submit %>
-
-<% end %>
-
-
-
-<%= link_to 'Edit Post', edit_post_path(@post) %> |
-<%= link_to 'Back to Posts', posts_path %> |
-
-
-现在你可以添加 posts 和 comments 到你的 blog 同时随后他们会在相应的地方显示出来。
-
-h3. 重构
-
-现在我们已经有Posts和Comments开始工作了,如果我们注意一下 +app/views/posts/show.html.erb+ 模板,发现它变得太长而且很别扭。我们可以使用 partials 来整理它。
-
-h4. Rendering Partial Collections
-
-首先我们会创建一个 comment partial 来专门显示 post 的所有的 comments。创建文件 +app/views/comments/_comment.html.erb+ 并输入如下内容:
-
-
-
- Commenter:
- <%= comment.commenter %>
-
-
-
- Comment:
- <%= comment.body %>
-
-
-
-然后在 +app/views/posts/show.html.erb+ 你可以相应的这样更改:
-
-
-<%= notice %>
-
-
- Name:
- <%= @post.name %>
-
-
-
- Title:
- <%= @post.title %>
-
-
-
- Content:
- <%= @post.content %>
-
-
-Comments
-<%= render @post.comments %>
-
-Add a comment:
-<%= form_for([@post, @post.comments.build]) do |f| %>
-
- <%= f.label :commenter %>
- <%= f.text_field :commenter %>
-
-
- <%= f.label :body %>
- <%= f.text_area :body %>
-
-
- <%= f.submit %>
-
-<% end %>
-
-
-
-<%= link_to 'Edit Post', edit_post_path(@post) %> |
-<%= link_to 'Back to Posts', posts_path %> |
-
-
-这里会把 +@post.comments+ 集合的每一个 comment 在 +app/views/comments/_comment.html.erb+ 模板中渲染。当渲染方法迭代完 @post.comments 集合时候,它分配每个 comment 为与 partial 名相同的本地变量(这里为comment),通过这样在 partial 中的 +comment+ 就可以显示给我们的用户了。
-
-h4. Rendering a Partial Form
-
-同样让我们移动 new comment 部分到它自己的地方吧。类似的,创建一个文件 +app/views/comments/_form.html.erb+ 并且在里面放入下面代码:
-
-
-<%= form_for([@post, @post.comments.build]) do |f| %>
-
- <%= f.label :commenter %>
- <%= f.text_field :commenter %>
-
-
- <%= f.label :body %>
- <%= f.text_area :body %>
-
-
- <%= f.submit %>
-
-<% end %>
-
-
-接着你这样修改 +app/views/posts/show.html.erb+ 文件:
-
-
-<%= notice %>
-
-
- Name:
- <%= @post.name %>
-
-
-
- Title:
- <%= @post.title %>
-
-
-
- Content:
- <%= @post.content %>
-
-
-Comments
-<%= render @post.comments %>
-
-Add a comment:
-<%= render "comments/form" %>
-
-
-
-<%= link_to 'Edit Post', edit_post_path(@post) %> |
-<%= link_to 'Back to Posts', posts_path %> |
-
-
-第二个 render 仅仅定义了一个我们想渲染的 partial template comments/form,Rails 可以智能的识别字符串代表的含义,并且知道你是想 render _form.html.erb模板在目录 app/views/comments 中。
-
-+@post+ 可以在任何的视图中partials rendered,因为我们把它定义成的实例变量。
-
-h3. Deleting Comments
-
-另一个重要的功能就是可以删除垃圾评论。要达到这样的效果,我们需要在view中实现某种链接和在 +CommentsController+ 中的 +DELETE+ 动作。
-
-首先,在 +app/views/comments/_comment.html.erb+ partial中添加delete link:
-
-
-
- Commenter:
- <%= comment.commenter %>
-
-
-
- Comment:
- <%= comment.body %>
-
-
-
- <%= link_to 'Destroy Comment', [comment.post, comment],
- :confirm => 'Are you sure?',
- :method => :delete %>
-
-
-
-点击 “Destroy Comment”,link 将会发送 DELETE
-/posts/:id/comments/:id 到我们的 +CommentsController+,+CommentsController+将会利用刚刚收到的(消息)找到我们想删除哪条评论,因此让我们接着添加一个 destroy action 到我们的 controller:
-
-
-class CommentsController < ApplicationController
-
- def create
- @post = Post.find(params[:post_id])
- @comment = @post.comments.create(params[:comment])
- redirect_to post_path(@post)
- end
-
- def destroy
- @post = Post.find(params[:post_id])
- @comment = @post.comments.find(params[:id])
- @comment.destroy
- redirect_to post_path(@post)
- end
-
-end
-
-
-+destroy+ action 将会找到那个我们正在阅读的 post,并且定位 comment 在 @post.comments 集合,然后从数据库 remove 它,最后传回消息到 post 的 show action。
-
-
-h4. Deleting Associated Objects
-
-如果你删除一个了 post,那么与之相关联的 comments 也需要被删除。否则他们将会只是在数据库中占用空间(别无它用)。Rails 允许你通过关系的依赖选项完成(上述功能)。修改Post model,+app/models/post.rb+,像这样:
-
-
-class Post < ActiveRecord::Base
- validates :name, :presence => true
- validates :title, :presence => true,
- :length => { :minimum => 5 }
- has_many :comments, :dependent => :destroy
-end
-
-
-h3. Security
-
-如果你就这样 publish 你的 blog 在互联网,任何人都可以添加,编辑和删除 post 或者删除 comments。
-
-Rails 提供了一个非常简单的 HTTP 认证系统在这样的情况下会非常适合。
-
-在 +PostsController+ 中我们需要一个方法来阻止没有通过认证的用户的操作,这里我们可以使用Rails的 http_basic_authenticate_with 这个方法,运行访问请求如果这个方法是允许的。
-
-要使用这个认证系统,我们需要在PostsController 的顶部在指定(即引用)它,这样我们希望用户在进行每个action的时候都是通过授权的,除了index和show,因此我们这样写:
-
-
-class PostsController < ApplicationController
-
- http_basic_authenticate_with :name => "dhh", :password => "secret", :except => [:index, :show]
-
- # GET /posts
- # GET /posts.json
- def index
- @posts = Post.all
- respond_to do |format|
-# snipped for brevity
-
-
-我们同样希望只有授权用户能够删除评论,因此在 +CommentsController+ 这样写:
-
-
-class CommentsController < ApplicationController
-
- http_basic_authenticate_with :name => "dhh", :password => "secret", :only => :destroy
-
- def create
- @post = Post.find(params[:post_id])
-# snipped for brevity
-
-
-现在如果你尝试创建一个新的 post,你将会迎来一个基于 HTTP 认证的挑战。
-
-!images/challenge.png(Basic HTTP Authentication Challenge)!
-
-h3. Building a Multi-Model Form
-
-另一个功能来平衡你的 blog 是能够给 posts 添加 tag。要想在你的程序中实现这个功能需要在一个 form 中与超过一个 model 互动。Rails 提供了嵌套 forms。
-
-为了演示这个(功能),你将会在你创建 post 的 form 中添加 post 的多 tag支持。首先创建一个 new model来存放 tags:
-
-
-$ rails generate model Tag name:string post:references
-
-
-再次运行 migration 来创建数据库表单:
-
-
-$ rake db:migrate
-
-
-接下来:编辑 +post.rb+ 文件来创建来创建另一个成员,并且告诉Rails(通过 the +accepts_nested_attributes_for+ 宏)你打算通过posts form来编辑tags。
-
-
-class Post < ActiveRecord::Base
- validates :name, :presence => true
- validates :title, :presence => true,
- :length => { :minimum => 5 }
-
- has_many :comments, :dependent => :destroy
- has_many :tags
- accepts_nested_attributes_for :tags, :allow_destroy => :true,
- :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
-end
-
-
-对于 +:allow_destroy+ 嵌套属性的声明是告诉 Rails 显示一个 “remove” 复选框在视图中那样你可以快速创建(tags)。对于 +:reject_if+ 保证不保存没有任何内容的tags。
-
-我们将要修改 +views/posts/_form.html.erb+ 来render(form的)一部分来创建tag:
-
-
-<% @post.tags.build %>
-<%= form_for(@post) do |post_form| %>
- <% if @post.errors.any? %>
-
-
<%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:
-
- <% @post.errors.full_messages.each do |msg| %>
- - <%= msg %>
- <% end %>
-
-
- <% end %>
-
-
- <%= post_form.label :name %>
- <%= post_form.text_field :name %>
-
-
- <%= post_form.label :title %>
- <%= post_form.text_field :title %>
-
-
- <%= post_form.label :content %>
- <%= post_form.text_area :content %>
-
- Tags
- <%= render :partial => 'tags/form',
- :locals => {:form => post_form} %>
-
- <%= post_form.submit %>
-
-<% end %>
-
-
-注意:我们已经更改 +form_for(@post) do |f|+ 中的 +f+ 为 +post_form+ 这样会更加容易明白是怎么回事。
-
-这个例子在 render helper 中使用另个方式,是为了说明我们希望的是在 +form+ 中使用局部变量指向的 +post_form+ 对象。
-
-我们还在form的顶部添加 @post.tags.build。这里是为了确保每个新的 tag 都被用户填上了 name。如果你不创建新 tag,form 将不会显示它。
-
-现在创建一个 app/views/tags 文件夹并且在里面新建一个 _form.html.erb 它包含tag form:
-
-
-<%= form.fields_for :tags do |tag_form| %>
-
- <%= tag_form.label :name, 'Tag:' %>
- <%= tag_form.text_field :name %>
-
- <% unless tag_form.object.nil? || tag_form.object.new_record? %>
-
- <%= tag_form.label :_destroy, 'Remove:' %>
- <%= tag_form.check_box :_destroy %>
-
- <% end %>
-<% end %>
-
-
-最后编辑 app/views/posts/show.html.erb 模板显示我们的 tags:
-
-
-<%= notice %>
-
-
- Name:
- <%= @post.name %>
-
-
-
- Title:
- <%= @post.title %>
-
-
-
- Content:
- <%= @post.content %>
-
-
-
- Tags:
- <%= @post.tags.map { |t| t.name }.join(", ") %>
-
-
-Comments
-<%= render @post.comments %>
-
-Add a comment:
-<%= render "comments/form" %>
-
-
-<%= link_to 'Edit Post', edit_post_path(@post) %> |
-<%= link_to 'Back to Posts', posts_path %> |
-
-
-通过这写修改,你会发现你可以直接在 post form中编辑 tags。
-
-另外, @post.tags.map { |t| t.name }.join(", ") 这个方法很别扭,我们可以通过编写一个helper method。
-
-h3. View Helpers
-
-View Helpers 放置在 app/helpers,它提供了可重用的小代码片段给 view。在本例,我们想要一个方法把(tag)放在一起(一个字符串中),并且使用逗号分割。要想这样在Post show 模板,我们在 PostHelper 中写入:
-
-现在你可以在 app/helpers/posts_helper.rb 中更改:
-
-
-module PostsHelper
- def join_tags(post)
- post.tags.map { |t| t.name }.join(", ")
- end
-end
-
-
-Now you can edit the view in app/views/posts/show.html.erb to look like
-this:
-
-
-<%= notice %>
-
-
- Name:
- <%= @post.name %>
-
-
-
- Title:
- <%= @post.title %>
-
-
-
- Content:
- <%= @post.content %>
-
-
-
- Tags:
- <%= join_tags(@post) %>
-
-
-Comments
-<%= render @post.comments %>
-
-Add a comment:
-<%= render "comments/form" %>
-
-
-<%= link_to 'Edit Post', edit_post_path(@post) %> |
-<%= link_to 'Back to Posts', posts_path %> |
-
-
-h3. What's Next?
-
-现在你已经看到了你的第一个 Rails 应用程序,你应该可以很轻松的继续更新它或者试验一下你的想法。当你在更新和运行 Rails 的时候需要援助,咨询下面推荐的资源会让你感到轻松:
-
-* The "Ruby on Rails guides":index.html
-* The "Ruby on Rails Tutorial":http://railstutorial.org/book
-* The "Ruby on Rails mailing list":http://groups.google.com/group/rubyonrails-talk
-* The "#rubyonrails":irc://irc.freenode.net/#rubyonrails channel on irc.freenode.net
-
-Rails 同样也带有内置的帮助你可以使用 rake 命令实用工具在你的应用程序中创建帮助文档:
-
-* 运行 +rake doc:guides+ 将会输出所有 Rails Guides 的文档到你的应用程序中的 +doc/guides+ 中。在你的浏览器中打开/guides/index.html浏览 Guides。
-* 运行 +rake doc:rails+ 将会输出所有Rails API 的文档到你的应用程序中的 +doc/api+ 中。在你的浏览器中打开 +doc/api/index.html+ 浏览API documentation。
-
-h3. Configuration Gotchas配置陷阱
-
-The easiest way to work with Rails is to store all external data as UTF-8. If
-you don't, Ruby libraries and Rails will often be able to convert your native
-data into UTF-8, but this doesn't always work reliably, so you're better off
-ensuring that all external data is UTF-8.
-使用 Rails 最简单的工作方式是存储所有的外部数据为 UTF-8编码。如果不那样做,Ruby libraries 和 Rails 通才会转换你的自然数据成 UTF-8 编码,但是这样不是很可靠,因此你最好保证所有的外部数据是 UTF-8 编码。如果你在这里犯了错误,一般的症状就是在浏览器中出现钻石符号(可能是^)变成了问号。 另一个普遍症状是 “ü” 变成了 “ü”。
-
-Rails 在国际化上下了很多的功夫,大部分的此类错误都能够自动发现并错误,然而如果你有一些不是用 UTF-8 存储的特殊的数据恐怕就会出现一些奇怪的问题了。
-
-两种典型的非 UTF-8 编码的源数据:
-* Your text editor: Most text editors (such as Textmate), default to saving files as
- UTF-8. If your text editor does not, this can result in special characters that you
- enter in your templates (such as é) to appear as a diamond with a question mark inside
- in the browser. This also applies to your I18N translation files.
- Most editors that do not already default to UTF-8 (such as some versions of
- Dreamweaver) offer a way to change the default to UTF-8. Do so.
-* Your database. Rails defaults to converting data from your database into UTF-8 at
- the boundary. However, if your database is not using UTF-8 internally, it may not
- be able to store all characters that your users enter. For instance, if your database
- is using Latin-1 internally, and your user enters a Russian, Hebrew, or Japanese
- character, the data will be lost forever once it enters the database. If possible,
- use UTF-8 as the internal storage of your database.
->>>>>>> f12ea7e98c8880c4f7e2a71dbd610c07bbb85977
diff --git a/source/documents_CN.yaml b/source/documents_CN.yaml
index d762070..b091daa 100755
--- a/source/documents_CN.yaml
+++ b/source/documents_CN.yaml
@@ -5,7 +5,7 @@
-
name: Rails 初上手指南
url: getting_started.html
- contributor: jhjguxin
+ contributor: jhjguxin, hozaka
description: 关于安装和建立一个全新的 Rails 程序你所需要知道的一切.
-
name: 模型(Models)