Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby on Rails | Controllerからlayoutの指定を行う

概要

Controllerからlayoutの指定を行う

内容

Controllerからlayoutの指定を利用します。

render layout: "layout_name"

サンプルコード

bookshelf_controller.rb

class BookshelfController < ApplicationController
  def index
    render layout: "html5_layout"
  end

  def click_link
    redirect_to :controller => 'help', :action => 'index'
  end

  def message
    render :text => "message test"
  end
end

html5_layout.html.haml

!!!
%html{:lang => "en"} 
  %head 
    %meta{:charset => "UTF-8"}/ 
    %title
      layout_test
  %body 
    layout_test
    = yield

結果

html5_layout.html.hamlを指定(layout_testの固定文字列が表示されている)

デフォルトのレイアウト(application.html.haml)のまま(layout_testの固定文字列が表示されていない)