I have a controller with the following layout logic
我有一个具有以下布局逻辑的控制器
layout 'sessions', :except => :privacy
layout 'static', :only => :privacy
The issue is that Rails seems to ignore the first line of code and the layout "sessions" is not applied for any actions. It simply thinks to render the static layout for privacy and no layout for the rest.
问题是Rails似乎忽略了第一行代码,并且布局“会话”不适用于任何操作。它只是认为为隐私呈现静态布局而其余部分没有布局。
Anyone know how to fix this?
有人知道怎么修这个东西吗?
3 个解决方案
#1
10
Another option is to define a method for your layout call, like so:
另一个选择是为布局调用定义一个方法,如下所示:
layout :compute_layout
and then
接着
def compute_layout
action_name == "privacy" ? "static" : "sessions"
end
However this is really only useful when you want to determine the layout at runtime based on some runtime parameter (like a variable being set). In your example, that does not seem to be necessary.
但是,当您想要在运行时根据某些运行时参数(如设置的变量)确定布局时,这实际上非常有用。在您的示例中,似乎没有必要。
#2
24
The reason this doesn't work is because you can only have global one layout declaration per controller. The :only
and :except
conditions just differentiate between actions that should get the specified layout and the ones that are excluded get rendered without a layout. In other words, a layout declaration always affects all actions that use default rendering.
这不起作用的原因是因为每个控制器只能有一个全局布局声明。 :only和:除了条件之外,只是区分应该获得指定布局的动作和被排除的动作在没有布局的情况下进行渲染。换句话说,布局声明始终会影响使用默认渲染的所有操作。
To override you simply specify a layout when you render like one of the following examples inside an action:
要覆盖您,只需在操作中执行以下示例之一时指定布局:
render :layout => 'static'
render :action => 'privacy', :layout => 'static'
render :layout => false # Don't render a layout
#3
2
You can just specify layout :static
where you need it.
您可以在需要的地方指定layout:static。
#1
10
Another option is to define a method for your layout call, like so:
另一个选择是为布局调用定义一个方法,如下所示:
layout :compute_layout
and then
接着
def compute_layout
action_name == "privacy" ? "static" : "sessions"
end
However this is really only useful when you want to determine the layout at runtime based on some runtime parameter (like a variable being set). In your example, that does not seem to be necessary.
但是,当您想要在运行时根据某些运行时参数(如设置的变量)确定布局时,这实际上非常有用。在您的示例中,似乎没有必要。
#2
24
The reason this doesn't work is because you can only have global one layout declaration per controller. The :only
and :except
conditions just differentiate between actions that should get the specified layout and the ones that are excluded get rendered without a layout. In other words, a layout declaration always affects all actions that use default rendering.
这不起作用的原因是因为每个控制器只能有一个全局布局声明。 :only和:除了条件之外,只是区分应该获得指定布局的动作和被排除的动作在没有布局的情况下进行渲染。换句话说,布局声明始终会影响使用默认渲染的所有操作。
To override you simply specify a layout when you render like one of the following examples inside an action:
要覆盖您,只需在操作中执行以下示例之一时指定布局:
render :layout => 'static'
render :action => 'privacy', :layout => 'static'
render :layout => false # Don't render a layout
#3
2
You can just specify layout :static
where you need it.
您可以在需要的地方指定layout:static。