Reputation: 1330
Here I've a controller with 4 actions and i wanna apply application layout ( the default layout ) to new action only , or in other words i wanna except only index action from the layout so i write this but it doesn't work, the index template is rendering with the layout.
class SessionsController < ApplicationController
layout 'application', :except => :index
def index
end
def new
end
def create
end
end
def destroy
end
end
also I tried
layout 'application', :only => :new
but it doesn't work too, same problem which the index template is rendering with layout. any suggestions what's the problem here?
Upvotes: 1
Views: 1332
Reputation: 694
Here's code that you can use in your controllers:
layout :resolve_layout
...
...
private
def resolve_layout
if %w(index show).include?(action_name)
"application"
elsif %w(show).include?(action_name)
"admin"
else
"generic"
end
Basically, treat those arrays as your :only statements for layout that you are giving them if they are evaluated as true when compared with current action_name.
EDIT: I forgot to mention that this allows :only, :except, etc.
Upvotes: 1
Reputation: 46914
You can create a method to define your layout and fix your layout only on new action
layout :my_layout
def my_layout
params[:action] == 'new' ? 'application' : nil
end
Upvotes: 3