method

layout

rails latest stable - Class: ActionController::Layout::ClassMethods

Method deprecated or moved

This method is deprecated or moved on the latest stable version. The last existing version (v2.3.8) is shown here.

layout(template_name, conditions = {}, auto = false)
public

If a layout is specified, all rendered actions will have their result rendered when the layout yields. This layout can itself depend on instance variables assigned during action performance and have access to them as any normal template would.

8Notes

Turn layout off

rubymaverick · Jun 30, 200812 thanks

If you don't want a layout rendered, just put:

layout nil

in your controller.

Function to Determine Layout

chs · Aug 7, 200812 thanks

Sometimes its nice to have different layouts choosen automagicly:

class ApplicationController < ActionController::Base layout :determine_layout def determine_layout if is_admin? "admin" else "application" end ... end

Required Reading

george · Jul 15, 20089 thanks

The details for using layout (such as possible values for the conditions hash) can be found in ActionController::Layout::ClassMethods.

Turn layout off with render

neves · Sep 17, 20084 thanks

Thats awkward, but the code below does not turn layout off:

render :action => "short_goal", :layout => nil

you must use false

render :action => "short_goal", :layout => false

Caveat when using dynamic layouts

mcmire · Feb 26, 20104 thanks

Worth noting that if you have a controller which inherits from another controller which has a layout, and in this child controller you're determining the layout at runtime using a method for specific actions, the other actions you are excluding will not inherit the layout from the parent controller.

For example, if you've got this

class BaseController < ApplicationController
layout "public"
end
class OrdersController < BaseController
layout :determine_layout, :only => :new
# index, show, new, create, edit, update, destroy ...
end

then OrdersController#index, #show, and #edit won't get the "public" layout -- in fact they won't get a layout at all. So you'll need to do this instead:

class OrdersController < BaseController
layout :determine_layout, :only => :new
layout "public", :except => :new
# ...
end

Using a specific layout

tomtt · Jul 4, 20083 thanks

To choose the layout (or no layout) for a method call the render method with a :layout option.

Re: Caveat when using dynamic layouts

mcmire · Mar 3, 20103 thanks

Since there's no way to edit posts on here, I need to correct myself and say that what I posted before doesn't work, since you can't specify layout multiple times:

class OrdersController < BaseController layout :determine_layout, :only => :new layout "public", :except => :new

...

end

So don't do that. The only way to ensure that the other actions get the default theme is to drop :only/:except and do the conditions yourself:

class OrdersController < BaseController layout :determine_layout

private def determine_layout %w(new).include?(action_name) ? "some_layout" : "public" end end

All this to say, beware of :only/:except -- they aren't as useful as you think they are.

Disable layout on ajax

kallahk · Mar 27, 20121 thank

In actions that may or may not be loaded via ajax I use: render :layout => !request.xhr? For an entire controller I might use something like: layout :has_layout?

private
def has_layout?
  request.xhr? ? false : controller_name
end

What seems unusual is that layout true will try look for the layout true.erb