[英]More than one layout for a controller
Is it possible to do something like this: 是否可以做这样的事情:
class SimulationController < ApplicationController
layout "generic", :only => [:login, :invalid]
layout "menubar", :except => [:login, :invalid]
For some reason, my login
page still uses the menubar layout (I can tell because a menu bar will appear at the top). 由于某种原因,我的login
页面仍然使用菜单栏布局(我可以知道,因为菜单栏将显示在顶部)。 If I comment out the 3rd line, the menu bar will disappear. 如果我注释掉第三行,菜单栏将消失。 So it seems like both layouts are being applied one after another. 因此,似乎两个布局都被一个接一个地应用。
But if I comment out the layout "generic"
bit, I will it will be just black and white, meaning no CSS style sheet is applied. 但是,如果我注释掉layout "generic"
位,那么它将是黑白的,这意味着未应用CSS样式表。
You look like you're trying to apply different layouts under different run-time conditions. 您看起来好像在尝试在不同的运行时条件下应用不同的布局。 The simplest way to tackle this is using a method reference for the layout. 解决此问题的最简单方法是对布局使用方法参考。
For example: - 例如: -
class ResourceController < ActionController::Base
layout :choose_layout
private
def choose_layout
if [ 'signup', 'login' ].include? action_name
'login_layout'
else
'admin_layout'
end
end
Check out the Rails API reference for ActionController::Layout under the heading "Types of Layout" 在标题“布局类型”下查看ActionController :: Layout的Rails API参考 。
看看这张票,您的问题似乎很相似:[ http://dev.rubyonrails.org/ticket/8867]
One way to do this is actually within your actions. 一种方法实际上是在您的行动之内。
def login
render :action => "login", :layout => "generic"
end
You could also make the actual "menubar" html a partial and turn the rendering off under certain conditions. 您也可以将实际的“ menubar” html局部化,并在某些条件下关闭渲染。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.