Ruby on Rails实践(6)---用Rails的方式编程
scaffold生成程序
在 Mybook 目录下打开 DOS 命令行窗口,运行 ruby script\generate model book

rails 生成 Model 类 Book 的骨架文件book.rb。 在这里 Rails 将数据库中的books 表映射到 Book 类。 这里 Rails 再次使用了习惯约定,那就是数据库表以复数形式命名,而与之对应的 Model 类,使用单数形式。这里 rails 非常智能,它理解英文的复数规则,会自动将 person 类和复数形式 people 数据库表做映射。
打开 book.rb
class Book < ActiveRecord::Base5:在 Mybook 目录下打开 DOS 命令行窗口,
end
运行 ruby script\generate controller book 生成 controller 骨架文件 book_controller.rb。
编辑 D:\railsdoc\mybook\app\controllers\ 下的 book_controller.rb
好了,魔法正式开场了。到现在我们还没有编写一行代码。如上所示
class BookController < ApplicationControllerscaffold :book – 这句是你惟一需要写的代码,它生成了数据库表对应的 CRUD 操作,而且令人激动的是它生成了对应的视图模板文件。
scaffold :book
end
我们来测试一下,在浏览器中打开 http://127.0.0.1:3000/book/new

整个数据库表的登记界面已经为你做好了,你现在连 html 页面都没有打开过,这就是 rails 给我们带来的神奇之处。我们试着登记一些信息。
你会看到我们已经拥有了 book 表的所有页面操作,show (显示表记录),edit (编辑表记录),destroy(删除表记录)。
scaffold :book 为我们完成了所有这一切,scaffold 是一个函数,它接受 :book 作为参数,这里 :book 是一个 symbol ,在 ruby 中, symbol 可以理解为变量名, 比如一个变量 book = 1 , 如果直接引用 book ,会得到 1,如果 :book,就指变量名本身。
scaffold :book 会生成 list, show, edit, delete 四个操作,对应表的操作。默认的操作页面很平常。如果我们想定制,也很简单,只要我们自定义自己的四个函数就可以了。
我们重新编辑 book_controller.rb 文件
class BookController < ApplicationController
scaffold :book
def list
end
end
刷新浏览器页面
提示说我们缺少模板文件。因为我们自定义了 list 函数,所以 rails 将不再使用 scaffold 给我们生成的 list 版本。所以我们必须编写自己的模板文件。
当我们用命令 ruby script\generate controller book 生成 controller 文件的时候, rails 同样在 view 目录下生成了 book 目录用于放置我们自己写的用于 controller 调用显示的模板文件。这里我们需要自己写一个 list.rhtml
代码如下:
<html>
<head>
<title>All books</title>
</head>
<body>
<h1>Online Mybook - All books</h1>
<table border="1">
<tr>
<td width="80%"><p align="center"><i><b>book</b></i></td>
<td width="20%"><p align="center"><i><b>Date</b></i></td>
</tr>
<% @books.each do |book| %>
<tr>
<td><%= link_to book.title, :action => "show", :id => book.id %></td>
<td><%= book.buydate %></td>
</tr>
<% end %>
</table>
<p><%= link_to "Create new book", :action => "new" %></p>
</body>
</html>
我们重新编辑 book_controller.rb 文件
class BookController < ApplicationController
scaffold :book
def list
@books = Book.find_all
end
end
再次重新刷新页面
OK 了,现在我们很容易的定制了列表页面。

在这里稍微做一些解释:
@books = Book.find_all
这条语句告诉 rails ,调用 Book 模型类中的 find_all 函数从数据库表 books 中把所有记录读出赋值到 @books 实例变量中。接着 rails 寻找 list.rhtml 以生成结果页面传递到浏览器页面显示。在 list.rhtml 中
<% @books.each do |book| %>
<tr>
<td><%= link_to book.title, :action => "show", :id => book.id %></td>
<td><%= book.buydate %></td>
</tr>
<% end %>
从 controller 中传递过来的 @books 变量调用 each iterator 循环边历 @books 中的数据
集。熟悉 asp, jsp 的人对上面的代码一定不陌生。这里使用的代码只不过是嵌入的ruby 代码而已。即使不太了解嵌入ruby 脚本,上面的代码也很直观。

Page Author
From Here You Can…
Information
- 400 Views
- 0 Comments
Most Recent Related Content
- Lesson
- Avatar

- Title
- Ruby on Rails实践(3)--- rails是什么 ?
- Body
- 在写这个教程的最初,我基本上是在翻译网上的教程。但是 rails中包...
- Author
- Lesson
- Avatar

- Title
- Action Controller: Streaming and file downloads
- Body
- Streaming and file downloads Sometimes you may want to send a file to the ...
- Author
- Video
- Avatar

- Title
- Ruby Basics - Session 6
- Description
- Author
- Video
- Avatar

- Title
- Ruby Idioms - Session 4
- Description
- Author
- Lesson
- Avatar

- Title
- How to use Fliqz4R
- Body
- About Fliqz Fliqz is the leader in full-service, plug-an...
- Author
- Lesson
- Avatar

- Title
- Upgrading non-trivial apps to Rails 2.1
- Body
- For simple Ruby on Rails apps that use very few plugins, upgrading may be as ...
- Author
- Lesson
- Avatar

- Title
- Using the Immutable Attribute Plugin
- Body
- Rails surprisingly does not include a validation method to make a model attri...
- Author
- Video
- Avatar

- Title
- Hack Autotest to clear Terminal after update
- Description
- I’ve gotten asked more than once how to clear terminal after autotest u...
- Author
Published In…
This work is public domain.
