简体   繁体   中英

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). 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.

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"

看看这张票,您的问题似乎很相似:[ 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM