2012-04-15 5 views
8

でコントローラごとに複数のレイアウトをレンダリング: Intro.html.hamlが正しく、私は私の<strong>Users_controller</strong>で定義したRailsの

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

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

はここに私のレイアウトはのように見えるものです

introを呼び出すページをレイアウトとしてレンダリングすると、の中にネストされます良くないファイルです。

レイアウトのこの望ましくないネスティングを避ける方法はありますか?

ありがとうございます!

答えて

41

問題は私のコントローラにありました。次のように複数のレイアウトインスタンスを宣言していました:

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

これをしないでください! 2番目の宣言が優先され、目的の効果が得られません。

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

それとも、それは少し複雑だ場合、あなたが処理を延期するシンボルを使用することができます:あなたのレイアウトは、単にアクション固有のものであれば

代わりに、ちょうどこのようなアクションの中にそれを宣言実行時のメソッド:

class UsersController < ApplicationController 
    layout :determine_layout 
    ... 

    private 
    def determine_layout 
     @current_user.admin? ? "admin" : "normal" 
    end 
end 
+0

ありがとうございました!私の顔を救った! –

関連する問題