Ruby on Rails Web开发实用指南
1. 布局文件的定义与使用
在Rails开发中,布局文件的使用能让开发者和设计者更清晰地把控整体页面结构。可以通过定义方法来确定布局文件,示例代码如下:
class FooController < ActionController:Base
layout :figure_out_layout
private
def figure_out_layout
if action_name =~ /pretty/
'pretty' # use pretty.rhtml for the layout
else
'standard' # use standard.rhtml
end
end
end
此外, layout 还支持传入一个lambda函数,以更简洁的代码动态决定布局:
class FooController < ActionController:Base
layout lambda { |controller| controller.logged_in? ? 'user' : 'guest' }
end
若需要使用显式的页眉和页脚,可以创建 app/views/layouts/_header.rhtml 和 app/views/layouts/_footer.rh
超级会员免费看
订阅专栏 解锁全文
9

被折叠的 条评论
为什么被折叠?



