Correct mapping of multiple layouts to a controller in Rails

I defined in my Users_controller :

layout "intro", only: [:new, :create]

Here's what my layout looks like: Intro.html.haml

 !!! 5 %html{lang:"en"} %head %title Intro = stylesheet_link_tag "application", :media => "all" = javascript_include_tag "application" = csrf_meta_tags %body{style:"margin: 0"} %header = yield %footer= debug(params) 

When I draw a page that calls intro as a layout, it becomes nested in my application.html.haml file, which is not good.

Is there any way to avoid this unwanted layout?

Thanks in advance!

+8
layout view render
source share
1 answer

The problem was in my controller. I declared multiple instances of the layout this way:

 class UsersController < ApplicationController layout "intro", only: [:new, :create] layout "full_page", only: [:show] ... end 

Do not do this! The second ad will take precedence, and you will not get the desired effect.

Instead, if your layouts are just action specific, simply declare them in the action as follows:

 def show ... render layout: "full_page" end 

Or, if it's a little more complicated, you can use a character to defer processing to a method at runtime as follows:

 class UsersController < ApplicationController layout :determine_layout ... private def determine_layout @current_user.admin? ? "admin" : "normal" end end 
+41
source share

All Articles