常用缩略语
CRUD:创建、读取、更新、删除
HTML:超文本标记语言
REST:具象状态传输
SQL:结构化查询语言
自 2007 年以来Rails 社区也已不断壮大全球有众多 Rails 开发人员充满活力、团结互助并且渴望不断地改进该软件Software可以毫不夸张地说Rails 改进应归功于社区是员的间不断相互超越使得构建出软件Software越来越完善如此不断地循环各个功能才得以从初期阶段快速发展逐渐变实用、功能不断强大并完善最终成为不可或缺工具在很多情况下社区认为最基本 gems 和 plug-ins 都会保留到 Rails 核心中Rails named scopes(个查询快捷方式)可实现和 nested forms 相同功能这是个新增功能可取代以前尝试并在相同 HTML 表单中创建和编辑多个模型实际上对于 Rails 开发人员来说最困难任务或许就是跟上变化节奏(幸运是目前有些 Ruby 和 Ruby _disibledevent=> respond_with(@post)
end
end
在该代码片段中如果 @post 保存成功respond_with(@post) 将发送到 show 以显示新记录而假设对象验证失败则发送到
这仅仅是个小样本您可以在下章节找到这些新功能举例以及更多内容例如从头构建 Rails 3 应用
首次构建 Rails 3 应用
要运行 Rails 3您系统必须安装有 Ruby 1.8.7 版或 Ruby 1.9.2 版或者该编程语言较新版本及其附加库和解释您机器上最好同时安装有 Git 软件Software版本控制系统 Rails 3 和许多其他重要 Rails 项目都是在 Git 中进行维护您系统还需要数据库引擎例如 SQLite(版本 3)、MySQL 或者 PostgreSQL开发 Rails 应用时Web 服务器不是必须但它通常是生产部署部分
要创建 Rails 3 应用您必须拥有 Rails 3 预发布 gem 和所有其相关产品这时您只需通过运行几条命令(请参阅 清单 1)即可安装所需组件(在您继续进行的前请查看 Rails 3 文档根据版本区别具体操作会有所区别)
清单 1. Rails 3 预发布 gem 和相关产品
$ gem rails3b
Due to a rubygems bug, you must un all older versions of bundler for 0.9 to work
Successfully ed mime-types-1.16
Successfully ed mail-2.1.2
Successfully ed text-hyphen-1.0.0
Successfully ed text-format-1.0.0
Successfully ed memcache-client-1.7.8
Successfully ed rack-1.1.0
Successfully ed rack-mount-0.4.7
Successfully ed abstract-1.0.0
Successfully ed erubis-2.6.5
Successfully ed i18n-0.3.3
Successfully ed tzinfo-0.3.16
Successfully ed bundler-0.9.5
Successfully ed thor-0.13.1
Successfully ed rails3b-3.0.1
14 gems ed
$ gem arel --pre
Successfully ed activesupport-3.0.0.beta
Successfully ed arel-0.2.pre
2 gems ed
$ gem rails --pre
Successfully ed activemodel-3.0.0.beta
Successfully ed actionpack-3.0.0.beta
Successfully ed activerecord-3.0.0.beta
Successfully ed activeresource-3.0.0.beta
Successfully ed actionmailer-3.0.0.beta
Successfully ed railties-3.0.0.beta
Successfully ed rails-3.0.0.beta
7 gems ed
下步是生成应用 — 在 清单 2 中显示了个小 wiki该应用创建并管理文章每篇文章都有个标题和些散文通过从现有页面正文创建个指向新文章引用您即可创建篇新文章引用可以是任驼峰式大小写单词例如 TheSolar 或者 TheOscars
注意:可通过下面 下载 表格获取该 wiki 应用源代码
清单 2. Wiki Rails 应用
$ rails wiki
如果您运行了 ls -lR 来查看应用内容将会显示些新文件:
Gemfile即前面曾提到 gem 清单该文件必须至少包含两行:行指向 Rails 3 beta gem 源另行则绑定 Rails 3 beta gem 本身您或许还需要第 3行(至少)以连接数据库:
source 'http://gemcutter.org'
gem "rails", "3.0.0.beta"
gem "sqlite3-ruby", :require => "sqlite3"
config/application.rb它包含 config/environment.rb 中以前提供很多选项虽然后者仍然保留但很大程度上已不再使用该文件config/application.rb 个显著附加功能是 generators block:
config.generators do |g|
g.orm :active_record
g.template_engine :erb
g.test_framework :test_unit, :fixture => true
end
您 Rails 3 应用可以使用些兼容对象关系映射器 (ORM)、模板引擎和测试框架生成器块会指定应用首选项并根据您模型、视图等适当生成器
db/seeds.rb该文件对于 Rails 3 来说并不是新增但却有必要着重介绍下它是最近不久刚增加功能(在 Rails 2.3.4 版引入)如果您应用需要数据以正常运行例如个管理用户、价格代码或静态页面那么您可以在 db/seeds.rb 中创建这些数据并运行任务 rake db:seed在 Seed 文件的前不存在化惯例许多开发人员把代码放入迁移中这样容易混淆创建数据库和填充数据库的间区别的处
config.ru存在于每个 Rails 3 应用根目录下即所谓 rackup 文件也就是基于 Rack 应用配置文件Rails 3 是个 Rack 应用并且和任支持 Rack Web 服务器相兼容总来说除非您想要添加其他 Rack 组件否则请不要更改 config.ru 文件
还有些其他新文件;不过大多数看上去和 Rails 版本 2.3 相似config/routes.rb 文件功能和以往相同只不过更加简化、更具有 Ruby 特色您将很快会看到个举例
生成应用并编辑 Gemfile 以声明依赖性的后下步就是收集应用所需 gem这是由新实用 bundle(请参阅 清单 3)来完成工作
清单 3. 收集所需 gem
$ bundle
Fetching source index from http://gemcutter.org
Resolving dependencies
Installing abstract (1.0.0) from system gems
Installing actionmailer (3.0.0.beta) from system gems
Installing actionpack (3.0.0.beta) from system gems
Installing activemodel (3.0.0.beta) from system gems
Installing activerecord (3.0.0.beta) from system gems
Installing activeresource (3.0.0.beta) from system gems
Installing activesupport (3.0.0.beta) from system gems
Installing arel (0.2.1) from rubygems repository at http://gemcutter.org
Installing builder (2.1.2) from system gems
Installing bundler (0.9.7) from rubygems repository at http://gemcutter.org
Installing erubis (2.6.5) from system gems
Installing i18n (0.3.3) from system gems
Installing mail (2.1.2) from system gems
Installing memcache-client (1.7.8) from system gems
Installing mime-types (1.16) from system gems
Installing rack (1.1.0) from system gems
Installing rack-mount (0.4.7) from system gems
Installing rack-test (0.5.3) from system gems
Installing rails (3.0.0.beta) from system gems
Installing railties (3.0.0.beta) from system gems
Installing rake (0.8.7) from system gems
Installing sqlite3-ruby (1.2.5) from rubygems repository at
http://gemcutter.org with native extensions
Installing text-format (1.0.0) from system gems
Installing text-hyphen (1.0.0) from system gems
Installing thor (0.13.3) from rubygems repository at http://gemcutter.org
Installing tzinfo (0.3.16) from system gems
Your bundle is complete!
该 bundle 实用简称 Bundler可用于下载和安装所有在 Gemfile 中指定 gem 以及任何这些 gems 依赖项(请参阅 清单 4)该 bundle 实用还可以将所有依赖项复制到您应用中使得您代码库自给自足具体来说如果您运行 bundle packBundler 会将所有 gem 资料复制到 vendor/cache
清单 4. 运行 bundle 实用
$ bundle pack
Copying .gem files o vendor/cache
* bundler-0.9.7.gem
* thor-0.13.3.gem
* abstract-1.0.0.gem
* mime-types-1.16.gem
* text-hyphen-1.0.0.gem
* rack-mount-0.4.7.gem
* rake-0.8.7.gem
* text-format-1.0.0.gem
* tzinfo-0.3.16.gem
* rack-test-0.5.3.gem
* builder-2.1.2.gem
* erubis-2.6.5.gem
* memcache-client-1.7.8.gem
* rack-1.1.0.gem
* sqlite3-ruby-1.2.5.gem
* i18n-0.3.3.gem
* activesupport-3.0.0.beta.gem
* arel-0.2.1.gem
* mail-2.1.2.gem
* activemodel-3.0.0.beta.gem
* activerecord-3.0.0.beta.gem
* actionpack-3.0.0.beta.gem
* railties-3.0.0.beta.gem
* actionmailer-3.0.0.beta.gem
* activeresource-3.0.0.beta.gem
* rails-3.0.0.beta.gem
$ ls vendor/cache
abstract-1.0.0.gem memcache-client-1.7.8.gem
actionmailer-3.0.0.beta.gem mime-types-1.16.gem
actionpack-3.0.0.beta.gem rack-1.1.0.gem
activemodel-3.0.0.beta.gem rack-mount-0.4.7.gem
activerecord-3.0.0.beta.gem rack-test-0.5.3.gem
activeresource-3.0.0.beta.gem rails-3.0.0.beta.gem
activesupport-3.0.0.beta.gem railties-3.0.0.beta.gem
arel-0.2.1.gem rake-0.8.7.gem
builder-2.1.2.gem sqlite3-ruby-1.2.5.gem
bundler-0.9.7.gem text-format-1.0.0.gem
erubis-2.6.5.gem text-hyphen-1.0.0.gem
i18n-0.3.3.gem thor-0.13.3.gem
mail-2.1.2.gem tzinfo-0.3.16.gem
将 vendor/cache 视为应用自己 gem 存储库您可以将代码库移动到任何地方并可以获得您所需 gem 软件Software和版本 — 无需远程存储器即可实现例如如果您在 bundle pack 的后运行 bundle gem 会从您应用存储库安装到您系统中(请参阅 清单 5)
清单 5. 安装 gem
Fetching source index from http://gemcutter.org
Resolving dependencies
Installing abstract (1.0.0) from .gem files at
/Users/strike/projects/rails3/wiki/vendor/cache
Installing actionmailer (3.0.0.beta) from .gem files at
/Users/strike/projects/rails3/wiki/vendor/cache
Installing actionpack (3.0.0.beta) from .gem files at
/Users/strike/projects/rails3/wiki/vendor/cache
...
Installing thor (0.13.3) from .gem files at
/Users/strike/projects/rails3/wiki/vendor/cache
Installing tzinfo (0.3.16) from .gem files at
/Users/strike/projects/rails3/wiki/vendor/cache
Your bundle is complete!
使用 wiki
要创建应用则需要为页面生成个工作框架(scaffold)、创建数据库、将页面放到数据库并且设定所需路径(请参阅 清单 6)为了简单化仅限在某些字段使用 wiki 页面记录:标题、标头(标题缩略语)、正文和时间截(以用于记录页面创建时间和最新修改时间)标题和标头是串字段;散文是文本字段;时间截是日期和时间字段(当然个真正 wiki 还会有其他字段如最近作者以及页面修订历史记录为了尽量简洁该例子还省略了用户和会话、格式以及各种身份验证和授权)您可以使用 rails generate scaffold 命令生成个模型、系列视图以及个控制器
清单 6. 完整 wiki 应用
$ rails generate scaffold page title: slug: body:text --timestamps
invoke active_record
create db/migrate/20100221115613_create_pages.rb
create app/models/page.rb
invoke test_unit
create test/unit/page_test.rb
create test/fixtures/pages.yml
route resources :pages
invoke scaffold_controller
create app/controllers/pages_controller.rb
invoke erb
create app/views/pages
create app/views/pages/index.html.erb
create app/views/pages/edit.html.erb
create app/views/pages/show.html.erb
create app/views/pages/.html.erb
create app/views/pages/_form.html.erb
create app/views/layouts/pages.html.erb
invoke test_unit
create test/functional/pages_controller_test.rb
invoke helper
create app/helpers/pages_helper.rb
invoke test_unit
create test/unit/helpers/pages_helper_test.rb
invoke stylesheets
create public/stylesheets/scaffold.css
如果您想知道 ./script/generate 命令有何变化回忆下该命令已经被全能 rails 命令包含了
运行 rake db:create db:migrate 以创建数据库:
$ rake db:create db:migrate
CreatePages: migrating
-- create_table(:pages)
-> 0.0010s
CreatePages: migrated (0.0011s) =
该 Wiki 现已存在但却是空添加个页面作为所有其他页面基准编辑文件 db/seeds.rb并编写代码以创建个新页面如 清单 7 中所示
清单 7. wiki 基准页面
Page.create(
:title => 'The Marx Brothers Wiki',
:slug => 'Home',
:body => 'An encyclopedic guide to the Marx Brothers.')
运行 rake db:seed 以执行代码您可以通过使用 rails console 快速浏览以验证页面如 清单 8 中所示
清单 8. 验证基准页面
$ rake db:seed
(in /Users/strike/projects/rails3/wiki)
$ rails console
Loading development environment (Rails 3.0.0.beta)
irb():001:0> Page.all
=> [#<Page id: 1, title: "The Marx Brothers Wiki", slug: "Home",
body: "An encyclopedic guide to the Marx Brothers.",
created_at: "2010-02-21 12:24:43", updated_at: "2010-02-21 12:24:43">]
在继续运行编码的前请先设定路径需要两条路径:条默认路径用来查找主页面而另外条路径则通过标头来查找页面清单 9 显示了最终版 config/routes.rb 文件
最新评论