Jump To Content

Ruby on Rails实践(7)---功能更深入一点

关联关系处理

定义如下:
在D:\railsdoc\mybook目录中打开 DOS 命令行,运行下列命令为 Category 生成模型和控制器类
ruby script\generate controller Category
ruby script\generate model Category

打开 D:\railsdoc\mybook\app\controllers 中自动生成的 category_controller.rb 编辑
class CategoryController < ApplicationController
         scaffold :category
end
          保存后,重新打开浏览器窗口,访问 http://127.0.0.1:3000/category/new。


          添加书籍类别数据


          为了让 books 数据表包含类别字段,我们需要新增加一个 category_id字段,字段定义为 int 型与 categories 表的 id 字段类型相同。



          打开 Model 目录中的两个 model 类文件book.rb 和 category.rb 进行编辑,各增加一行
book.rb 修改如下:
class Book < ActiveRecord::Base
      belongs_to :category
end

这行代码告诉 Rails, 一本书属于一个类别
category.rb 修改如下:
class Category < ActiveRecord::Base
       has_many :books
end
        这行代码告诉 Rails, 一个类别可以包含很多本书。
        这两行申明就可以给我们生成在表关系之间导航的各种方法。例如,@book 变量中保存了一本书的信息,我们想知道它属于的类别名,我就可以使用代码 @book.category.name , 再比如,@category 保存了一个类别,我们想知道这个类别下有多少本书,那么我们可以通过 @category.books 来获得。
现在我们从新定义 edit 函数,来覆盖它的 scalffold 版本。
编辑 book_controller.rb 文件
class BookController < ApplicationController
       scaffold :book
    def list
        @books = Book.find_all
     end
    def edit
        @book = Book.find(@params["id"])
        @categories = Category.find_all
     end
end
      edit 函数中定义的两行代码产生两个实例变量,这两个变量 @book 和 @categories 将在 edit book 页面中用到, @book 变量存放我们要编辑的书本对象, id 参数取自网页的访问请求。 @categories 变量存放数据表 categories 中所包含的所有记录。
     因为我们已经从新定义了 edit action ,那么我们同样需要自己编写 edit.rhtml。 和前面 list action 对应 list.rhtml 类似。
打开 D:\railsdoc\mybook\app\views\book 目录 ,新建一个 edit.rhtml 输入如下内容
<html>
<head>
    <title>Edit book</title>
</head>
<body>
     <h1>Edit book</h1>
     <form action="../update" method="POST">
        <input id="book_id" name="book[id]" size="30"
        type="hidden" value="<%= @book.id %>" />
     <p><b>Title</b><br>
       <input id="book_title" name="book[title]" size="30"
       type="text" value="<%= @book.title %>" />
     </p>
    <p><b>Description</b><br>
     <input id="book_description" name="book[description]"
     size="30" type="text"  value="<%= @book.description %>" />
    </p>

   <p><b>Category:</b><br>
   <select name="book[category_id]">
    <% @categories.each do |category| %>
   <option value="<%= category.id %>"
    <%= ' selected' if category.id == @book.category.id %>>
   <%= category.name %>
    </option>
   <% end %>
    </select></p>


    <input type="submit" value="Update" />
  </form>
   <a href="/book/show/<%= @book.id %>">
    Show
   </a> |
    <a href="/book/list">
    Back
     </a>
   </body>
</html>

       注意斜体的内容,我们正是通过编辑 book_controller.rb 文件中的 edit 函数建立的变量 categories 来循环生成“类别”下来菜单内容。
      打开浏览器,http://127.0.0.1:3000/book/list 点击任和一条记录进入编辑页面,选择 edit 按钮,显示如下编辑页面。


      选择相应类别,点击 update 按钮。重新编辑修改现有的两个记录,从而在 books 表记录中增加类别信息。
我们现在编辑的 list.rhtml 并没有包含 category 显示栏目,现在从新加上
从新打开 llist.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>书名</b></i></td>
<td width="20%"><p align="center"><i><b>类别</b></i></td>
<td width="20%"><p align="center"><i><b>购买日期</b></i></td>
</tr>
<% @books.each do |book| %>
<tr>
<td><%= link_to book.title, :action => "show", :id => book.id %></td>
<td><%= book.category.name %></td>
<td><%= book.buydate %></td>
</tr>
<% end %>
</table>
<p><%= link_to "Create new book", :action => "new" %></p>
</body>
</html>

从新刷新浏览器内容(http://127.0.0.1:3000/book/list)。



我们开始定义的三个任务已经完成。

  • Your comment will be modifiable for 10 minutes after posted.

Page Author

Avatar
val
Name
val

From Here You Can…

Information

Most Recent Related Content

Published In…

This work is public domain.