views:

423

answers:

3

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.

Anyone know how to fix this?

+2  A: 

You can just specify layout :static where you need it.

Jim
+4  A: 

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.

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
dasil003
+3  A: 

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.

JRL
this is what i resorted to... +1 for the "action_name"...didn't know about that
Tony
action_name is also easy to throw into a case/when/then block when you have to deal with more than 2 actions.
Jared