随笔-167  评论-65  文章-0  trackbacks-0
一:给Views下的所有的rhtml页面统一的装饰
定义layouts下的application.rhtml 即可

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<!-- 连接CSS样式文件 -->
<link rel="stylesheet" href="/stylesheets/message.css" type="text/css" media="all"/>
<!-- 包含系统中默认的JavaScript库 -->
<%= javascript_include_tag :defaults %>
<title>===欢迎光临留言系统===</title>
</head>
<body>
<br/>
<br/>
<br/>
<div id="user-page">        
<div id="header-section">
ROR简单留言系统
<%= @username %>
</div>
<div id="content-section">
<!-- 调用yield来返回被装饰页面中的内容 -->
<%= yield %>
</div>
<div id="footer-section">
CopyRight @ 2009 RiskFIT,INC  Feng
</div>
</div>
</body>
</html>

红色部分就是调用被装饰的页面了.其它为公共部分

二:给views下的一个文件夹下的rhtml装饰
例如controller是 user_controller.rb 则按照rails的约定,对应的layout是layouts下的message.rhtml 对应的view在views目录下的message文件夹下rhtml
1,如果没有定义layouts下的application.rhtml 则就按照message.rhtml装饰
2,如果application.rhtml 和 message.rhtml同时定义 则就同时都执行装饰 (似乎没有人这么傻)

三:给controller对应下的action指定layout
在controller中加入
Layout “message.rhtml” 即可
例如:

class MessageController < ApplicationController

layout("message.rhtml",:only=>'new')

End
:only 表示只对new action起作用,
还有 :except 表示对哪些action不起作用

四:给单个action指定特定的 layout
因为一个controller内不能导入多个layout 所以这种做法很重要

在controller中定义action 只需这样写即可

 def new
@message = Message.new
render :action =>'new',:layout=>"message.rhtml"
end
红色部分即是跳转到action用的layout了.


write by feng
posted on 2009-03-20 18:13 fl1429 阅读(1059) 评论(0)  编辑  收藏 所属分类: Rails

只有注册用户登录后才能发表评论。


网站导航:
 
已访问数:
free counters