简体   繁体   English

控制器有多个布局

[英]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.

相关问题 Rails 中的每个 controller 有超过 2 个动态布局选项 - More than 2 dynamic layout options per controller in Rails 如何在Rails的ruby中的一个控制器中执行多个更新 - how to do more than one updates in one controller in ruby on rails 控制器动作包含多个模型方法调用? - controller action contains more than one model method call? 将多个动作路由到同一个 controller 和动作 - Routing more than one action to the same controller and action JQuery UI 自动完成多个 controller 的 DRY 解决方案? - A DRY solution of JQuery UI Autocomplete for more than one controller? 我如何为多个 controller 导轨 6 布线方法 - How can i route a mothod for more than one controller rails 6 在 ruby​​ on rails 中,我可以在一个控制器中添加多个资源吗? - In ruby on rails, can I add more than one resources in one controller? Rails:如何从一个控制器的动作渲染多个部分? - Rails: How to render more than one partial from one controller's action? 如何实现控制器以处理一个或多个记录的创建? - How to implement controller in order to handle the creation of one or more than one record? 我如何称呼超过一个字的刺激 controller? - How do I call a Stimulus controller that's more than one word?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM