项目开发(八)
来源:互联网 发布:制作小游戏的软件 编辑:程序博客网 时间:2024/05/22 06:09
项目开发(八)
修改category.rb
一切,都比较流畅,尽管不完美。
关联表的操作实现
1、增加关联
1、在books表中增加category_id字段,实现关联。
2、在model目录中,修改book.rb
1、在books表中增加category_id字段,实现关联。
2、在model目录中,修改book.rb
belongs_to:category
这行代码告诉 Rails, 一本书属于一个类别
这行代码告诉 Rails, 一本书属于一个类别
修改category.rb
has_many:books
这行代码告诉 Rails, 一个类别可以包含很多本书。
这两行申明就可以给我们生成在表关系之间导航的各种方法.
3、测试,并没有改变。
4、定义 edit 函数
def edit
@book=Book.find(@params["id"])
@category=Category.find_all
end
因为我们已经从新定义了 edit action ,那么我们同样需要自己编写 edit.rhtml.
5、edit.rhtml
<html>
<head>
<title>Edit book</title>
</head>
<body>
<h1>Edit book</h1>
<form action="../update/<%=@book.id%>" method="POST">
这行代码告诉 Rails, 一个类别可以包含很多本书。
这两行申明就可以给我们生成在表关系之间导航的各种方法.
3、测试,并没有改变。
4、定义 edit 函数
def edit
@book=Book.find(@params["id"])
@category=Category.find_all
end
因为我们已经从新定义了 edit action ,那么我们同样需要自己编写 edit.rhtml.
5、edit.rhtml
<html>
<head>
<title>Edit book</title>
</head>
<body>
<h1>Edit book</h1>
<form action="../update/<%=@book.id%>" 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>
<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>
<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>
</form>
<a href="/book/show/<%= @book.id %>"> Show </a>
|
<a href="/book/list"> Back </a>
</body>
</html>
|
<a href="/book/list"> Back </a>
</body>
</html>
6、修改list.rhtml,显示 category 栏目
<html>
<head>
<title>All books</title>
</head>
<body>
<h1>Online Mybook - All books</h1>
<html>
<head>
<title>All books</title>
</head>
<body>
<h1>Online Mybook - All books</h1>
<table border="1" width="100%">
<tr>
<td width="50%"><p align="center"><i><b>book</b></i></td>
<td width="30%"><p align="center"><i><b>Category</b></i></td>
<td width="20%"><p align="center"><i><b>Date</b></i></td>
</tr>
<tr>
<td width="50%"><p align="center"><i><b>book</b></i></td>
<td width="30%"><p align="center"><i><b>Category</b></i></td>
<td width="20%"><p align="center"><i><b>Date</b></i></td>
</tr>
<% @books.each do |book| %>
<tr align="center">
<td ><%=link_to book.title,:action => "show", :id => book.id %></td>
<td><%= book.category.name %></td>
<td><%= book.buydate %></td>
</tr>
<% end %>
<tr align="center">
<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>
<%= link_to "Create new book", :action => "new" %>
</p>
</body>
</html>
</html>
7、测试
当测试,book/new时,出错,因为添加记录是,category_id字段没有,为空,出错。
因为,在category_id默认值为0,但在添加和显示时,0无法从categories表中读出,出错。
把设置为4,其他,则成功。
1、利用数据库中,来形成规则检查。
2、在代码中填加检查,来形成规则。
当测试,book/new时,出错,因为添加记录是,category_id字段没有,为空,出错。
因为,在category_id默认值为0,但在添加和显示时,0无法从categories表中读出,出错。
把设置为4,其他,则成功。
1、利用数据库中,来形成规则检查。
2、在代码中填加检查,来形成规则。
一切,都比较流畅,尽管不完美。
- 项目开发(八)
- 项目开发第二阶段周报四(第七、八周)
- 木雨音乐 项目开发(八)最近播放界面
- ios项目(八)
- 项目进度(八)
- 开发一个好项目:八、创建view
- python实践项目(八)
- 【potatoes游戏开发】cocos2dx3.X项目重写(八)观察者模式(中)
- 【potatoes游戏开发】cocos2dx3.X项目重写(八)观察者模式(中)
- react项目实战(权限模块开发八)js文件分开打包
- 从零开发一个完整的Android项目(八)——水平气泡显示
- Cocos2d开发系列(八)
- Cocos2d开发系列(八)
- play framework2开发(八)
- ios开发(八):property
- CRM开发要点(八)
- java基础开发(八)
- 软件项目管理之八:开发测试:强化薄弱环节
- 项目开发(五)
- MySQL-Front-4.1注册码
- ARP协议工作原理
- 项目开发(六)
- 项目开发(七)
- 项目开发(八)
- 初次学习RUBY的总结
- 可能产生LNK2001错误的原因:
- 收集一下名言警句来激励自己。。。。
- 关于HashTable的小知识点
- [原创]GRUB引导逻辑分区WINDOWS
- 用协处理器提高uC/OS,Il的实时性
- 一个基于Win32的完全面向对象的窗口类
- Clamav杀毒软件源码分析笔记[三]